当前位置:Gxlcms > css > CSS、JS实现浪漫流星雨动画

CSS、JS实现浪漫流星雨动画

时间:2021-07-01 10:21:17 帮助过:53人阅读

本篇文章给大家带来的内容是关于CSS 、JS实现浪漫流星雨动画,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。效果如下:

微信截图_20181113172841.png

HTML

由于节点很多,并且我想尽量做得逼真有趣有点,还给节点加了随机位置。所以节点的输出都是用JS控制的,HTML这边只写了几个父元素盒子,加上相应的ID名和类类名,结构相对简单。

CSS

CSS部分的难点就是流星的样式和用圈圈画云层,然后将云层堆叠出立体效果。【推荐阅读:CSS堆叠上下文是什么?有什么作用?

首先说一下流星的样式:

  1. #sky .star { position:absolute;
  2. 不透明度:0 ;
  3. z-index:10000 ;
  4. }
  5. .star :: after { content:“” ;
  6. 显示:块;
  7. 边界:坚固; border-width:2px 0 2px 80px ;
  8. / *流星随长度逐渐缩小* /
  9. border-color:透明透明透明rgba(255,255,255,1); border-radius:2px 0 0 2px ; transform:rotate(-45deg); transform-origin:0 0 0 ;
  10. 盒子阴影:0 0 20px rgba(255,255,255,.3);
  11. }

先提取了公共样式,添加定位属性;

然后在星后通过后伪类添加流星,用边界特性画:

1)模型绘制:border-width的顺序为四边top,right,bottom,left,同理border-color的顺序也为四边top,right,bottom,left。这样将border-width与border-color一一对应后,就能看出2px的是流星的宽度,80px是流星的长度,而0像素流星就是尾巴的这样就形成了一个。头部2px的宽,尾部0像素,长度80px的流星模型 ;

2)稍微逼真:通过边界半径?给流星的头部增加个圆角,让它看起来更逼真最后通过roteta旋转一个角度,让它看起来像是往下掉;

3)增加闪光:通过箱阴影给流星增加一点光晕,让它看起来有闪光的效果;

通过以上3步,一个流星就画好了。

然后是画云:

因为云的代码比较长,这里就不贴出来了方法无非是通过一个一个的圆,相互叠加覆盖,完成一个云朵的形状。
完成一个云层之后,copy一个,然后多个云层通过rotate,opacity,left定位等,做出一个渐隐叠加的立体效果;

JS

JS部分以流星举例说明:

  1. setInterval(function() {
  2. const obj = addChild(“#sky”,“div”,2,“star”); //插入流星
  3. for(let i = 0 ; i <obj.children.length; i ++){ //随机位置
  4. const top = -50 + Math .random()* 200 + “px”,
  5. left = 200 + Math .random()* 1200 + “px”,
  6. scale = 0.3 + Math .random()* 0.5 ;
  7. const timer = 1000 + Math .random()* 1000 ;
  8. obj.children [i] .style.top = top;
  9. obj.children [i] .style.left = left;
  10. obj.children [i] .style.transform = `scale($ {scale})` ;
  11. //添加动画
  12. requestAnimation({
  13. ele:obj.children [i],
  14. attr:[ “top”,“left”,“opacity” ],
  15. 值:[ 150,-150,.8 ],
  16. time:timer,
  17. flag:false,
  18. fn:function() {
  19. requestAnimation({
  20. ELE:obj.children [I],
  21. ATTR:“顶”,“左”,“不透明” ],
  22. 值:[ 150,-150,0 ],
  23. 时间:定时器,
  24. 标志:假,
  25. FN:() => {
  26. obj.parent.removeChild(obj.children [I]); //动画结束删除节点
  27. }
  28. })
  29. }
  30. });
  31. }
  32. },1000);

这里边用到了我自己封装的两个方法,一个是基于requestAnimationFrame的requestAnimation,以及基于appendChild的addChild。

为了达成星星位置随机的效果,通过定时器的setInterval的不停插入与删除流星:

首先,每次添加2个流星到页面,但是定时器的间隔时间小于流星的动画时间,这样就能保证页面中的流星的数量不是一个固定值,但肯定是大于2的。不然一次2个流星略显冷清;

然后,通过对循环(也可以用为式,换的,都行。对于-的最简单)给每个新添加到页面中的流星一个随机的位置(顶部,左侧),随机的大小(规模),随机的动画执行时间(定时器);

最后,在用于循环中,给每个新添加到页面中的流星加上动画,并通过回调函数在执行完动画后删除节点。这里要注意的是,要动画分成两个阶段(出现与消失,主要是opacity控制)。另外我这里的处理,每个流星都移动相同的距离300px,这个距离我觉得也可以通过随机数控制,但我犯了个懒,就没有做。

附上代码:

HTML:

  1. < body >
  2. < div class = “container” >
  3. < div id = “mask” > </ div >
  4. < div id = “sky” > </ div >
  5. < div id = “moon” > </ div >
  6. < div id = “stars” > </ div >
  7. < div class = “cloud cloud-1” ></ div >
  8. <div class = “cloud cloud-2” > </ div >
  9. < div class = “cloud cloud-3” > </ div >
  10. </ div >
  11. </ body >

css:

  1. /* - - - - - - 重启 - - - - - - */
  2. * {
  3. 保证金:0 ;
  4. 填充:0 ;
  5. }
  6. html,
  7. body { width:100% ;
  8. 最小宽度:1000px ;
  9. 身高:100% ;
  10. 最小高度:400px ;
  11. 溢出:隐藏;
  12. }
  13. / * ------------画布------------ * /
  14. .container { position:relative;
  15. 身高:100% ;
  16. }
  17. / *遮罩层* /
  18. #mask { position:absolute;
  19. 宽度:100% ;
  20. 身高:100% ; background:rgba(0,0,0,.8);
  21. z-index:900 ;
  22. }
  23. / *天空背景* /
  24. #sky { width:100% ;
  25. 身高:100% ; background:线性渐变(rgba(0,150,255,1),rgba(0,150,255,.8),rgba(0,150,255,.5));
  26. }
  27. / *月亮* /
  28. #moon { position:absolute;
  29. 上:50px ;
  30. 右:200px ;
  31. 宽度:120px ;
  32. 身高:120px ;
  33. 背景:rgba(251,255,25,0.938); border-radius:50% ; box-shadow:0 0 20px rgba(251,255,25,0.5);
  34. z-index:9999 ;
  35. }
  36. / *闪烁星星* /
  37. .blink { position:absolute; background:rgb(255,255,255); border-radius:50% ; box-shadow:0 0 5px rgb(255,255,255);
  38. 不透明度:0 ;
  39. z-index:10000 ;
  40. }
  41. / *流星* /
  42. .star { position:absolute;
  43. 不透明度:0 ;
  44. z-index:10000 ;
  45. }
  46. .star :: after { content:“” ;
  47. 显示:块;
  48. 边界:坚固; border-width:2px 0 2px 80px ;
  49. / *流星随长度逐渐缩小* /
  50. border-color:透明透明透明rgba(255,255,255,1); border-radius:2px 0 0 2px ; transform:rotate(-45deg); transform-origin:0 0 0 ;
  51. 盒子阴影:0 0 20px rgba(255,255,255,.3);
  52. }
  53. / *云* /
  54. .cloud { position:absolute;
  55. 宽度:100% ;
  56. 身高:100px ;
  57. }
  58. .cloud-1 {
  59. bottom: - 100px ;
  60. z-index:1000 ;
  61. 不透明度:1 ;
  62. 变换:规模(1.5);
  63. -webkit-transform:scale(1.5);
  64. -moz-transform:scale(1.5);
  65. -ms-transform:scale(1.5);
  66. -o-transform:scale(1.5);
  67. }
  68. .cloud-2 {
  69. left: - 100px ;
  70. 底部: - 50px ;
  71. z-index:999 ;
  72. 不透明度:。5 ;
  73. 变换:旋转(7deg);
  74. -webkit-transform:rotate(7deg);
  75. -moz-transform:rotate(7deg);
  76. -ms-transform:rotate(7deg);
  77. -o-transform:rotate(7deg);
  78. }
  79. .cloud-3 {
  80. left:120px ;
  81. 底部: - 50px ;
  82. z-index:999 ;
  83. 不透明度:。1 ; transform:rotate(-10deg);
  84. -webkit-transform:rotate(-10deg);
  85. -moz-transform:rotate(-10deg);
  86. -ms-transform:rotate(-10deg);
  87. -o-transform:rotate(-10deg);
  88. }
  89. .circle { position:absolute; border-radius:50% ;
  90. 背景:#fff ;
  91. }
  92. .circle-1 { width:100px ;
  93. 身高:100px ;
  94. 上: - 50px ;
  95. 左:10px ;
  96. }
  97. .circle-2 { width:150px ;
  98. 身高:150px ;
  99. 上: - 50px ;
  100. 左:30px ;
  101. }
  102. .circle-3 { width:300px ;
  103. 身高:300px ;
  104. 上: - 100px ;
  105. 左:80px ;
  106. }
  107. .circle-4 { width:200px ;
  108. 身高:200px ;
  109. 上: - 60px ;
  110. 左:300px ;
  111. }
  112. .circle-5 { width:80px ;
  113. 身高:80px ;
  114. 上: - 30px ;
  115. 左:450px ;
  116. }
  117. .circle-6 { width:200px ;
  118. 身高:200px ;
  119. 上: - 50px ;
  120. 左:500px ;
  121. }
  122. .circle-7 { width:100px ;
  123. 身高:100px ;
  124. 上: - 10px ;
  125. 左:650px ;
  126. }
  127. .circle-8 { width:50px ;
  128. 身高:50px ;
  129. 上:30px ;
  130. 左:730px ;
  131. }
  132. .circle-9 { width:100px ;
  133. 身高:100px ;
  134. 上:30px ;
  135. 左:750px ;
  136. }
  137. .circle-10 { width:150px ;
  138. 身高:150px ;
  139. 上:10px ;
  140. 左:800px ;
  141. }
  142. .circle-11 { width:150px ;
  143. 身高:150px ;
  144. 上: - 30px ;
  145. 左:850px ;
  146. }
  147. .circle-12 { width:250px ;
  148. 身高:250px ;
  149. 上: - 50px ;
  150. 左:900px ;
  151. }
  152. .circle-13 { width:200px ;
  153. 身高:200px ;
  154. 上: - 40px ;
  155. 左:1000px ;
  156. }
  157. .circle-14 { width:300px ;
  158. 身高:300px ;
  159. 上: - 70px ;
  160. 左:1100px ;

JS:

  1. //流星动画
  2. setInterval(function() {
  3. const obj = addChild(“#sky”,“div”,2,“star”); for(let i = 0 ; i <obj.children.length; i ++){
  4. const top = -50 + Math .random()* 200 + “px”,
  5. left = 200 + Math .random()* 1200 + “px”, scale = 0.3 + Math .random()* 0.5 ;
  6. const timer = 1000 + Math .random()* 1000 ;
  7. obj.children [i] .style.top = top;
  8. obj.children [i] .style.left = left;
  9. obj.children [i] .style.transform = `scale($ {scale})` ;
  10. requestAnimation({
  11. ele:obj.children [i],
  12. attr:[ “top”,“left”,“opacity” ],
  13. 值:[ 150,-150,.8 ], time:timer,
  14. flag:false,
  15. fn:function() {
  16. requestAnimation({
  17. ELE:obj.children [I],
  18. ATTR:“顶”,“左”,“不透明” ],
  19. 值:[ 150,-150,0 ],
  20. 时间:定时器,
  21. 标志:假,
  22. FN:() => {
  23. obj.parent.removeChild(obj.children [I]);
  24. }
  25. })
  26. }
  27. });
  28. }
  29. },1000);
  30. //闪烁星星动画
  31. setInterval(function() {
  32. const obj = addChild(“#stars”,“div”,2,“blink”); for(let i = 0 ; i <obj.children.length; i ++){
  33. const top = -50 + Math .random()* 500 + “px”,
  34. left = 200 + Math .random()* 1200 + “px”, round = 1 + Math .random()* 2 + “px” ;
  35. const timer = 1000 + Math .random()* 4000 ;
  36. obj.children [i] .style.top = top;
  37. obj.children [i] .style.left = left;
  38. obj.children [i] .style.width = round;
  39. obj.children [i] .style.height = round;
  40. requestAnimation({
  41. ele:obj.children [i],
  42. attr:“opacity”,
  43. 值:.5, time:timer,
  44. flag:false,
  45. fn:function() {
  46. requestAnimation({
  47. ele:obj.children [i],
  48. attr:“opacity”,
  49. value:0, time:timer,
  50. flag:false,
  51. fn:function() {
  52. obj.parent.removeChild(obj.children [I]);
  53. }
  54. });
  55. }
  56. });
  57. }
  58. },1000);
  59. //月亮移动
  60. requestAnimation({
  61. ele:“#moon”,
  62. attr:“right”,
  63. 值:1200,
  64. 时间:10000000,
  65. });
  66. //添加云
  67. const clouds = addChild(“。cloud”,“div”,14,“circle”,true);for(let i = 0 ; i <clouds.children.length; i ++){ for(let j = 0 ; j <clouds.children [i] .length;){
  68. clouds.children [i] [j] .classList.add(`circle- $ {++ j} `);
  69. }
  70. }
  71. //云动画let flag = 1 ;
  72. 的setInterval(
  73. 功能() {
  74. const clouds = document .querySelectorAll(“。cloud”);
  75. const left = Math .random()* 5 ;
  76. bottom = Math .random()* 5 ; let timer = 0 ; for(let i = 0 ; i <clouds.length; i ++){
  77. requestAnimation({
  78. ele:clouds [i],
  79. attr:[ “left”,“bottom” ],
  80. value:flag%2?[-left,-bottom]:[left,bottom], time:timer + = 500,
  81. flag:false,
  82. fn:function() {
  83. requestAnimation({
  84. ele:clouds [i],
  85. attr:[ “left”,“bottom” ],
  86. value:flag%2?[left,bottom]:[ - left,-bottom], time:timer,
  87. flag:false
  88. })
  89. }
  90. });
  91. }
  92. 标志++;
  93. },2000)

以上就是对CSS 、JS实现浪漫流星雨动画的全部介绍,如果您想了解更多有关CSS教程,请关注PHP中文网。

以上就是CSS 、JS实现浪漫流星雨动画的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行