当前位置:   article > 正文

学习CSS3,模拟春雪漫天飘的动画效果_css 雪夜动画

css 雪夜动画

清明时节雨纷纷,但有些地方却下起了大雪,今天我们就用所学的CSS3知识,模拟一下夜晚漫天飘雪的场景吧。

目录

1. 实现思路

2. 部分HTML代码 

3. 夜空的背景 

4. 雪花的样式 

5. 粒子飞升效果 

6. HTML完整源代码 

7. CSS3完整源代码

8.  最后 


1. 实现思路

  • DIV布局的使用
  • 整体背景radial-gradient属性的使用
  • 夜空rotate属性的使用
  • 雪花radial-gradient属性的使用
  • 雪花移动动画animation属性的使用
  • 雪花移动过程中translate3d属性的使用

2. 部分HTML代码 

因为雪花的元素是相同的,只是移动的起点移动过程移动的终点不同,所以HTML元素大致相同,这里我们就不把所有的元素都粘贴过来了,稍后会粘贴出所有源代码,你可以拿到源代码放到自己的网页里,即可看到漫天飘雪的场景啦。

  1. <div class="container">
  2. <div class="circle-container">
  3. <div class="circle"></div>
  4. </div>
  5. <div class="circle-container">
  6. <div class="circle"></div>
  7. </div>
  8. <div class="circle-container">
  9. <div class="circle"></div>
  10. </div>
  11. ......
  12. <!-- 此处重复此处越多,效果越好 -->
  13. </div>

3. 夜空的背景 

夜空为了绚烂一些,肯定不是能是纯黑色,需要做一定的过渡效果,雪花飘落才会更完美,这里用到了background-image: radial-gradient  等CSS属性

  1. body {
  2. background-image: radial-gradient(#021027, #000000);
  3. }
  4. .container {
  5. width: 100%;
  6. height: 100%;
  7. overflow: hidden;
  8. transform: rotate(180deg);
  9. }

4. 雪花的样式 

雪花虽然HTML元素相同,但表现形式却不同。他有自己的大小明暗移动轨迹,等等,越随机,才能越表现的真实而完美

  1. .circle-container .circle {
  2. width: 100%;
  3. height: 100%;
  4. border-radius: 50%;
  5. mix-blend-mode: screen;
  6. background-image: radial-gradient(#99ffff, #99ffff 10%, rgba(153, 255, 255, 0) 56%);
  7. -webkit-animation: fadein-frames 200ms infinite, scale-frames 2s infinite;
  8. animation: fadein-frames 200ms infinite, scale-frames 2s infinite;
  9. }
  10. @-webkit-keyframes scale-frames {
  11. 0% {
  12. -webkit-transform: scale3d(0.4, 0.4, 1);
  13. transform: scale3d(0.4, 0.4, 1);
  14. }
  15. 50% {
  16. -webkit-transform: scale3d(2.2, 2.2, 1);
  17. transform: scale3d(2.2, 2.2, 1);
  18. }
  19. 100% {
  20. -webkit-transform: scale3d(0.4, 0.4, 1);
  21. transform: scale3d(0.4, 0.4, 1);
  22. }
  23. }

5. 粒子飞升效果 

可能在第3步,大家看到了 transform: rotate(180deg); 的代码设置,这是做了另外的考虑。满天飞雪的场景,其实如果旋转屏幕,可以做为那种地面上有某种粒子,逐渐向上飞升的效果,也是非常棒的。喜欢的小伙伴可以试一下。

6. HTML完整源代码 

下面把完整源代码放出来,需要的小伙伴可以直接COPY过去,放到自己网页上就可以看到满天飞雪的效果啦

  1. <!DOCTYPE html>
  2. <html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  3. <title>漫天飘雪</title>
  4. <link rel="stylesheet" href="./style.css">
  5. </head>
  6. <body>
  7. <div class="container">
  8. <div class="circle-container">
  9. <div class="circle"></div>
  10. </div>
  11. <div class="circle-container">
  12. <div class="circle"></div>
  13. </div>
  14. <div class="circle-container">
  15. <div class="circle"></div>
  16. </div>
  17. <div class="circle-container">
  18. <div class="circle"></div>
  19. </div>
  20. <div class="circle-container">
  21. <div class="circle"></div>
  22. </div>
  23. <div class="circle-container">
  24. <div class="circle"></div>
  25. </div>
  26. <div class="circle-container">
  27. <div class="circle"></div>
  28. </div>
  29. <div class="circle-container">
  30. <div class="circle"></div>
  31. </div>
  32. <div class="circle-container">
  33. <div class="circle"></div>
  34. </div>
  35. <div class="circle-container">
  36. <div class="circle"></div>
  37. </div>
  38. <div class="circle-container">
  39. <div class="circle"></div>
  40. </div>
  41. <div class="circle-container">
  42. <div class="circle"></div>
  43. </div>
  44. <div class="circle-container">
  45. <div class="circle"></div>
  46. </div>
  47. <div class="circle-container">
  48. <div class="circle"></div>
  49. </div>
  50. <div class="circle-container">
  51. <div class="circle"></div>
  52. </div>
  53. <div class="circle-container">
  54. <div class="circle"></div>
  55. </div>
  56. </div>
  57. </body></html>

7. CSS3完整源代码

  1. html,
  2. body {
  3. width: 100%;
  4. height: 100%;
  5. padding:0;margin:0;
  6. }
  7. body {
  8. background-image: radial-gradient(#021027, #000000);
  9. }
  10. .container {
  11. width: 100%;
  12. height: 100%;
  13. overflow: hidden;
  14. transform: rotate(180deg);
  15. }
  16. .circle-container {
  17. position: absolute;
  18. -webkit-transform: translateY(-10vh);
  19. transform: translateY(-10vh);
  20. -webkit-animation-iteration-count: infinite;
  21. animation-iteration-count: infinite;
  22. -webkit-animation-timing-function: linear;
  23. animation-timing-function: linear;
  24. }
  25. .circle-container .circle {
  26. width: 100%;
  27. height: 100%;
  28. border-radius: 50%;
  29. mix-blend-mode: screen;
  30. background-image: radial-gradient(#99ffff, #99ffff 10%, rgba(153, 255, 255, 0) 56%);
  31. -webkit-animation: fadein-frames 200ms infinite, scale-frames 2s infinite;
  32. animation: fadein-frames 200ms infinite, scale-frames 2s infinite;
  33. }
  34. @-webkit-keyframes scale-frames {
  35. 0% {
  36. -webkit-transform: scale3d(0.4, 0.4, 1);
  37. transform: scale3d(0.4, 0.4, 1);
  38. }
  39. 50% {
  40. -webkit-transform: scale3d(2.2, 2.2, 1);
  41. transform: scale3d(2.2, 2.2, 1);
  42. }
  43. 100% {
  44. -webkit-transform: scale3d(0.4, 0.4, 1);
  45. transform: scale3d(0.4, 0.4, 1);
  46. }
  47. }
  48. @keyframes scale-frames {
  49. 0% {
  50. -webkit-transform: scale3d(0.4, 0.4, 1);
  51. transform: scale3d(0.4, 0.4, 1);
  52. }
  53. 50% {
  54. -webkit-transform: scale3d(2.2, 2.2, 1);
  55. transform: scale3d(2.2, 2.2, 1);
  56. }
  57. 100% {
  58. -webkit-transform: scale3d(0.4, 0.4, 1);
  59. transform: scale3d(0.4, 0.4, 1);
  60. }
  61. }
  62. .circle-container:nth-child(1) {
  63. width: 10px;
  64. height: 10px;
  65. -webkit-animation-name: move-frames-1;
  66. animation-name: move-frames-1;
  67. -webkit-animation-duration: 8441ms;
  68. animation-duration: 8441ms;
  69. -webkit-animation-delay: 4544ms;
  70. animation-delay: 4544ms;
  71. }
  72. @-webkit-keyframes move-frames-1 {
  73. from {
  74. -webkit-transform: translate3d(50vw, 102vh, 0);
  75. transform: translate3d(50vw, 102vh, 0);
  76. }
  77. to {
  78. -webkit-transform: translate3d(2vw, -117vh, 0);
  79. transform: translate3d(2vw, -117vh, 0);
  80. }
  81. }
  82. @keyframes move-frames-1 {
  83. from {
  84. -webkit-transform: translate3d(50vw, 102vh, 0);
  85. transform: translate3d(50vw, 102vh, 0);
  86. }
  87. to {
  88. -webkit-transform: translate3d(2vw, -117vh, 0);
  89. transform: translate3d(2vw, -117vh, 0);
  90. }
  91. }
  92. .circle-container:nth-child(1) .circle {
  93. -webkit-animation-delay: 3734ms;
  94. animation-delay: 3734ms;
  95. }
  96. .circle-container:nth-child(2) {
  97. width: 10px;
  98. height: 10px;
  99. -webkit-animation-name: move-frames-2;
  100. animation-name: move-frames-2;
  101. -webkit-animation-duration: 9921ms;
  102. animation-duration: 9921ms;
  103. -webkit-animation-delay: 5982ms;
  104. animation-delay: 5982ms;
  105. }
  106. @-webkit-keyframes move-frames-2 {
  107. from {
  108. -webkit-transform: translate3d(89vw, 108vh, 0);
  109. transform: translate3d(89vw, 108vh, 0);
  110. }
  111. to {
  112. -webkit-transform: translate3d(72vw, -123vh, 0);
  113. transform: translate3d(72vw, -123vh, 0);
  114. }
  115. }
  116. @keyframes move-frames-2 {
  117. from {
  118. -webkit-transform: translate3d(89vw, 108vh, 0);
  119. transform: translate3d(89vw, 108vh, 0);
  120. }
  121. to {
  122. -webkit-transform: translate3d(72vw, -123vh, 0);
  123. transform: translate3d(72vw, -123vh, 0);
  124. }
  125. }
  126. .circle-container:nth-child(2) .circle {
  127. -webkit-animation-delay: 2516ms;
  128. animation-delay: 2516ms;
  129. }
  130. .circle-container:nth-child(3) {
  131. width: 3px;
  132. height: 3px;
  133. -webkit-animation-name: move-frames-3;
  134. animation-name: move-frames-3;
  135. -webkit-animation-duration: 10427ms;
  136. animation-duration: 10427ms;
  137. -webkit-animation-delay: 3649ms;
  138. animation-delay: 3649ms;
  139. }
  140. @-webkit-keyframes move-frames-3 {
  141. from {
  142. -webkit-transform: translate3d(85vw, 107vh, 0);
  143. transform: translate3d(85vw, 107vh, 0);
  144. }
  145. to {
  146. -webkit-transform: translate3d(30vw, -133vh, 0);
  147. transform: translate3d(30vw, -133vh, 0);
  148. }
  149. }
  150. @keyframes move-frames-3 {
  151. from {
  152. -webkit-transform: translate3d(85vw, 107vh, 0);
  153. transform: translate3d(85vw, 107vh, 0);
  154. }
  155. to {
  156. -webkit-transform: translate3d(30vw, -133vh, 0);
  157. transform: translate3d(30vw, -133vh, 0);
  158. }
  159. }
  160. .circle-container:nth-child(3) .circle {
  161. -webkit-animation-delay: 731ms;
  162. animation-delay: 731ms;
  163. }
  164. .circle-container:nth-child(4) {
  165. width: 6px;
  166. height: 6px;
  167. -webkit-animation-name: move-frames-4;
  168. animation-name: move-frames-4;
  169. -webkit-animation-duration: 10951ms;
  170. animation-duration: 10951ms;
  171. -webkit-animation-delay: 8909ms;
  172. animation-delay: 8909ms;
  173. }
  174. @-webkit-keyframes move-frames-4 {
  175. from {
  176. -webkit-transform: translate3d(50vw, 104vh, 0);
  177. transform: translate3d(50vw, 104vh, 0);
  178. }
  179. to {
  180. -webkit-transform: translate3d(74vw, -122vh, 0);
  181. transform: translate3d(74vw, -122vh, 0);
  182. }
  183. }
  184. @keyframes move-frames-4 {
  185. from {
  186. -webkit-transform: translate3d(50vw, 104vh, 0);
  187. transform: translate3d(50vw, 104vh, 0);
  188. }
  189. to {
  190. -webkit-transform: translate3d(74vw, -122vh, 0);
  191. transform: translate3d(74vw, -122vh, 0);
  192. }
  193. }
  194. .circle-container:nth-child(4) .circle {
  195. -webkit-animation-delay: 2526ms;
  196. animation-delay: 2526ms;
  197. }
  198. .circle-container:nth-child(5) {
  199. width: 5px;
  200. height: 5px;
  201. -webkit-animation-name: move-frames-5;
  202. animation-name: move-frames-5;
  203. -webkit-animation-duration: 7642ms;
  204. animation-duration: 7642ms;
  205. -webkit-animation-delay: 2502ms;
  206. animation-delay: 2502ms;
  207. }
  208. @-webkit-keyframes move-frames-5 {
  209. from {
  210. -webkit-transform: translate3d(9vw, 108vh, 0);
  211. transform: translate3d(9vw, 108vh, 0);
  212. }
  213. to {
  214. -webkit-transform: translate3d(39vw, -126vh, 0);
  215. transform: translate3d(39vw, -126vh, 0);
  216. }
  217. }
  218. @keyframes move-frames-5 {
  219. from {
  220. -webkit-transform: translate3d(9vw, 108vh, 0);
  221. transform: translate3d(9vw, 108vh, 0);
  222. }
  223. to {
  224. -webkit-transform: translate3d(39vw, -126vh, 0);
  225. transform: translate3d(39vw, -126vh, 0);
  226. }
  227. }
  228. .circle-container:nth-child(5) .circle {
  229. -webkit-animation-delay: 2755ms;
  230. animation-delay: 2755ms;
  231. }
  232. .circle-container:nth-child(6) {
  233. width: 6px;
  234. height: 6px;
  235. -webkit-animation-name: move-frames-6;
  236. animation-name: move-frames-6;
  237. -webkit-animation-duration: 8439ms;
  238. animation-duration: 8439ms;
  239. -webkit-animation-delay: 455ms;
  240. animation-delay: 455ms;
  241. }
  242. @-webkit-keyframes move-frames-6 {
  243. from {
  244. -webkit-transform: translate3d(29vw, 101vh, 0);
  245. transform: translate3d(29vw, 101vh, 0);
  246. }
  247. to {
  248. -webkit-transform: translate3d(21vw, -109vh, 0);
  249. transform: translate3d(21vw, -109vh, 0);
  250. }
  251. }
  252. @keyframes move-frames-6 {
  253. from {
  254. -webkit-transform: translate3d(29vw, 101vh, 0);
  255. transform: translate3d(29vw, 101vh, 0);
  256. }
  257. to {
  258. -webkit-transform: translate3d(21vw, -109vh, 0);
  259. transform: translate3d(21vw, -109vh, 0);
  260. }
  261. }
  262. .circle-container:nth-child(6) .circle {
  263. -webkit-animation-delay: 3506ms;
  264. animation-delay: 3506ms;
  265. }
  266. .circle-container:nth-child(7) {
  267. width: 8px;
  268. height: 8px;
  269. -webkit-animation-name: move-frames-7;
  270. animation-name: move-frames-7;
  271. -webkit-animation-duration: 7539ms;
  272. animation-duration: 7539ms;
  273. -webkit-animation-delay: 3595ms;
  274. animation-delay: 3595ms;
  275. }
  276. @-webkit-keyframes move-frames-7 {
  277. from {
  278. -webkit-transform: translate3d(11vw, 101vh, 0);
  279. transform: translate3d(11vw, 101vh, 0);
  280. }
  281. to {
  282. -webkit-transform: translate3d(31vw, -125vh, 0);
  283. transform: translate3d(31vw, -125vh, 0);
  284. }
  285. }
  286. @keyframes move-frames-7 {
  287. from {
  288. -webkit-transform: translate3d(11vw, 101vh, 0);
  289. transform: translate3d(11vw, 101vh, 0);
  290. }
  291. to {
  292. -webkit-transform: translate3d(31vw, -125vh, 0);
  293. transform: translate3d(31vw, -125vh, 0);
  294. }
  295. }
  296. .circle-container:nth-child(7) .circle {
  297. -webkit-animation-delay: 749ms;
  298. animation-delay: 749ms;
  299. }
  300. .circle-container:nth-child(8) {
  301. width: 4px;
  302. height: 4px;
  303. -webkit-animation-name: move-frames-8;
  304. animation-name: move-frames-8;
  305. -webkit-animation-duration: 7480ms;
  306. animation-duration: 7480ms;
  307. -webkit-animation-delay: 2680ms;
  308. animation-delay: 2680ms;
  309. }
  310. @-webkit-keyframes move-frames-8 {
  311. from {
  312. -webkit-transform: translate3d(15vw, 101vh, 0);
  313. transform: translate3d(15vw, 101vh, 0);
  314. }
  315. to {
  316. -webkit-transform: translate3d(88vw, -111vh, 0);
  317. transform: translate3d(88vw, -111vh, 0);
  318. }
  319. }
  320. @keyframes move-frames-8 {
  321. from {
  322. -webkit-transform: translate3d(15vw, 101vh, 0);
  323. transform: translate3d(15vw, 101vh, 0);
  324. }
  325. to {
  326. -webkit-transform: translate3d(88vw, -111vh, 0);
  327. transform: translate3d(88vw, -111vh, 0);
  328. }
  329. }
  330. .circle-container:nth-child(8) .circle {
  331. -webkit-animation-delay: 1888ms;
  332. animation-delay: 1888ms;
  333. }
  334. .circle-container:nth-child(9) {
  335. width: 2px;
  336. height: 2px;
  337. -webkit-animation-name: move-frames-9;
  338. animation-name: move-frames-9;
  339. -webkit-animation-duration: 9087ms;
  340. animation-duration: 9087ms;
  341. -webkit-animation-delay: 9461ms;
  342. animation-delay: 9461ms;
  343. }
  344. @-webkit-keyframes move-frames-9 {
  345. from {
  346. -webkit-transform: translate3d(100vw, 107vh, 0);
  347. transform: translate3d(100vw, 107vh, 0);
  348. }
  349. to {
  350. -webkit-transform: translate3d(40vw, -130vh, 0);
  351. transform: translate3d(40vw, -130vh, 0);
  352. }
  353. }
  354. @keyframes move-frames-9 {
  355. from {
  356. -webkit-transform: translate3d(100vw, 107vh, 0);
  357. transform: translate3d(100vw, 107vh, 0);
  358. }
  359. to {
  360. -webkit-transform: translate3d(40vw, -130vh, 0);
  361. transform: translate3d(40vw, -130vh, 0);
  362. }
  363. }
  364. .circle-container:nth-child(9) .circle {
  365. -webkit-animation-delay: 1721ms;
  366. animation-delay: 1721ms;
  367. }
  368. .circle-container:nth-child(10) {
  369. width: 8px;
  370. height: 8px;
  371. -webkit-animation-name: move-frames-10;
  372. animation-name: move-frames-10;
  373. -webkit-animation-duration: 9860ms;
  374. animation-duration: 9860ms;
  375. -webkit-animation-delay: 8969ms;
  376. animation-delay: 8969ms;
  377. }
  378. @-webkit-keyframes move-frames-10 {
  379. from {
  380. -webkit-transform: translate3d(74vw, 110vh, 0);
  381. transform: translate3d(74vw, 110vh, 0);
  382. }
  383. to {
  384. -webkit-transform: translate3d(30vw, -127vh, 0);
  385. transform: translate3d(30vw, -127vh, 0);
  386. }
  387. }
  388. @keyframes move-frames-10 {
  389. from {
  390. -webkit-transform: translate3d(74vw, 110vh, 0);
  391. transform: translate3d(74vw, 110vh, 0);
  392. }
  393. to {
  394. -webkit-transform: translate3d(30vw, -127vh, 0);
  395. transform: translate3d(30vw, -127vh, 0);
  396. }
  397. }
  398. .circle-container:nth-child(10) .circle {
  399. -webkit-animation-delay: 1801ms;
  400. animation-delay: 1801ms;
  401. }
  402. .circle-container:nth-child(11) {
  403. width: 1px;
  404. height: 1px;
  405. -webkit-animation-name: move-frames-11;
  406. animation-name: move-frames-11;
  407. -webkit-animation-duration: 9292ms;
  408. animation-duration: 9292ms;
  409. -webkit-animation-delay: 9812ms;
  410. animation-delay: 9812ms;
  411. }

8.  最后 

最后呢,祝大家2023年心想事成

  • 【手把手、从零到一】SpringBoot+SpringCloud+Vue前后端分离实战项目,专栏持续火热更新中。。。
  • 主流技术,细节到位,前后端由两位【十年多】的高级架构师操刀
  • 作为毕设项目、入门项目、或者准备进阶提升竞争力的小伙伴,可以【订阅本专栏】哦
  • 前端部分https://blog.csdn.net/xingyu_qie/category_12222258.html
  • 服务端部分https://blog.csdn.net/scm_2008/category_12236048.html
  • 粉丝福利:订阅的粉丝可加微信,对文章的内容进行【一对一指导】!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/207659
推荐阅读
相关标签
  

闽ICP备14008679号