效果图

1.gif

css

  1. <style>
  2. /*//漂浮物*/
  3. @keyframes mysnow {
  4. 0% {
  5. bottom: 100%;
  6. transform: rotate(0deg);
  7. }
  8. 100% {
  9. transform: rotate(-30deg);
  10. bottom: -10%;
  11. }
  12. }
  13. @-webkit-keyframes mysnow {
  14. 0% {
  15. bottom: 100%;
  16. -webkit-transform: rotate(0deg);
  17. }
  18. 100% {
  19. -webkit-transform: rotate(-30deg);
  20. bottom: -10%;
  21. }
  22. }
  23. @-moz-keyframes mysnow {
  24. 0% {
  25. bottom: 100%;
  26. -moz-transform: rotate(0deg);
  27. }
  28. 100% {
  29. -moz-transform: rotate(-30deg);
  30. bottom: -10%;
  31. }
  32. }
  33. @-ms-keyframes mysnow {
  34. 0% {
  35. bottom: 100%;
  36. -ms-transform: rotate(0deg);
  37. }
  38. 100% {
  39. -ms-transform: rotate(-30deg);
  40. bottom: -10%;
  41. }
  42. }
  43. @-o-keyframes mysnow {
  44. 0% {
  45. bottom: 100%;
  46. -o-transform: rotate(00deg);
  47. }
  48. 100% {
  49. -o-transform: rotate(-30deg);
  50. bottom: -10%;
  51. }
  52. }
  53. .roll5 {
  54. position: absolute;
  55. animation: mysnow 20s linear;
  56. -webkit-animation: mysnow 20s linear;
  57. -moz-animation: mysnow 20s linear;
  58. -ms-animation: mysnow 20s linear;
  59. -o-animation: mysnow 20s linear;
  60. }
  61. .roll4 {
  62. position: absolute;
  63. animation: mysnow 12s linear;
  64. -webkit-animation: mysnow 12s linear;
  65. -moz-animation: mysnow 12s linear;
  66. -ms-animation: mysnow 12s linear;
  67. -o-animation: mysnow 12s linear;
  68. }
  69. .roll3 {
  70. position: absolute;
  71. animation: mysnow 8s ease-out;
  72. -webkit-animation: mysnow 8s ease-out;
  73. -moz-animation: mysnow 8s ease-out;
  74. -ms-animation: mysnow 8s ease-out;
  75. -o-animation: mysnow 8s ease-out;
  76. }
  77. .roll {
  78. position: fixed;
  79. z-index: 9999999;
  80. }
  81. </style>

js

  1. <script>
  2. /*漂浮物*/
  3. function snow(left, top, src) {
  4. var img = document.createElement("img");
  5. img.className = "roll roll"+ Math.floor(Math.random() * 3 + 3) + "";
  6. img.src = src;
  7. img.style.width = Math.random()*(1.2-0.6)+0.6 + "rem";
  8. img.style.height = "auto";
  9. img.style.left = left + "px";
  10. img.style.bottom = "100%";
  11. document.getElementsByTagName("body")[0].appendChild(img);
  12. setTimeout(function() {
  13. document.getElementsByTagName("body")[0].removeChild(img);
  14. }, 20000);
  15. }
  16. setInterval(function() {
  17. var left = Math.random() * window.innerWidth;
  18. var top =0;
  19. // var src = "images/gift" + Math.floor(Math.random() * 6 + 1) + ".png";
  20. var src = "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2742336169,1171788391&fm=26&gp=0.jpg";
  21. snow(left, top, src);
  22. }, 1000);
  23. /*漂浮物end*/
  24. </script>

不需要 div,直接添加在 body 里面了