image.png

  1. <template>
  2. <view class="page">
  3. <view
  4. v-for="(item, index) in Array(18).fill()"
  5. :key="index"
  6. class="animation span"
  7. :data-animation-position="position()"
  8. :data-animation-delay="delay()"
  9. :data-animation-timing="timing()"
  10. :data-animation-duration="duration()"
  11. :data-animation-name="name()"
  12. ><image class="image" src="../../static/bonus.png"
  13. /></view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {};
  20. },
  21. onReady: function() {},
  22. methods: {
  23. random(min, max) {
  24. return Math.floor(Math.random() * (max - min + 1) + min);
  25. },
  26. position() {
  27. return this.random(1, 100);
  28. },
  29. delay() {
  30. return this.random(1, 4);
  31. },
  32. duration() {
  33. return this.random(4, 8);
  34. },
  35. name() {
  36. return this.random(1, 4);
  37. },
  38. timing() {
  39. return ["linear", "ease", "ease-in", "ease-out", "ease-in-out"][
  40. this.random(0, 4)
  41. ];
  42. }
  43. }
  44. };
  45. </script>
  46. <style lang="scss" scoped>
  47. html,
  48. body,
  49. #app {
  50. height: 100%;
  51. background: #000;
  52. }
  53. .page {
  54. height: 100vh;
  55. overflow: hidden;
  56. background: #000;
  57. position: relative;
  58. color: #fff;
  59. .span {
  60. width: 30upx;
  61. height: 30upx;
  62. position: absolute;
  63. top: -100upx;
  64. animation-iteration-count: infinite;
  65. transform-origin: center -30upx;
  66. transform: translate3d(0, 0, 0);
  67. }
  68. .image {
  69. width: 30upx;
  70. height: 30upx;
  71. }
  72. }
  73. // 横向起始位置
  74. @for $i from 1 through 100 {
  75. .animation[data-animation-position="#{$i}"] {
  76. left: #{$i}vw;
  77. }
  78. }
  79. // 降落曲线
  80. $timing: (
  81. linear: linear,
  82. ease: ease,
  83. ease-in: ease-in,
  84. ease-out: ease-out,
  85. ease-in-out: ease-in-out
  86. );
  87. @each $key, $value in $timing {
  88. .animation[data-animation-timing="#{$key}"] {
  89. transition-timing-function: $value;
  90. }
  91. }
  92. // 延时时间
  93. @for $i from 1 through 4 {
  94. .animation[data-animation-delay="#{$i}"] {
  95. animation-delay: #{$i}s;
  96. }
  97. }
  98. // 持续时间
  99. @for $i from 4 through 8 {
  100. .animation[data-animation-duration="#{$i}"] {
  101. animation-duration: #{$i}s;
  102. }
  103. }
  104. // 动画
  105. @for $i from 1 through 4 {
  106. .animation[data-animation-name="#{$i}"] {
  107. animation-name: fall, swing#{$i}, scaleing#{$i};
  108. }
  109. }
  110. // 下落
  111. @keyframes fall {
  112. 0% {
  113. top: -80upx;
  114. opacity: 1;
  115. }
  116. 50% {
  117. opacity: 0.7;
  118. }
  119. 100% {
  120. top: 55vh;
  121. color: red;
  122. opacity: 0.3;
  123. }
  124. }
  125. // 左右摇摆
  126. @for $i from 1 through 4 {
  127. @keyframes swing#{$i} {
  128. 25% {
  129. transform: translateX(-#{$i * 20}px);
  130. }
  131. 50% {
  132. transform: translateX(#{$i * 20}px);
  133. }
  134. 75% {
  135. transform: translateX(-#{$i * 20}px);
  136. }
  137. 100% {
  138. transform: translateX(#{$i * 20}px);
  139. }
  140. }
  141. }
  142. // 透视
  143. @for $i from 1 through 4 {
  144. @keyframes scaleing#{$i} {
  145. from {
  146. transform: scale($i*0.6);
  147. }
  148. to {
  149. transform: scale($i * 1.5);
  150. }
  151. }
  152. }
  153. </style>

Note

  1. 主要是css属性选择器配合自定义data-属性的使用
  2. css没有随机数,用scss循环生成多个类,用属性选择器去匹配

参考:
https://juejin.im/post/5c4525ab6fb9a049bb7ca45c