当前位置:Gxlcms > JavaScript > vue移动端实现红包雨效果

vue移动端实现红包雨效果

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

本文实例为大家分享了vue实现红包雨效果的具体代码,供大家参考,具体内容如下

下面是代码:

  1. <template>
  2. <div class="ser_home">
  3. <ul class="red_packet" id="red_packet">
  4. <template v-for="(item, index) in liParams">
  5. <li :style="{ left: item.left, animationDuration: item.durTime, webkitAnimationDuration: item.durTime}"
  6. :class="item.cls" :data-index="index" @webkitAnimationEnd="removeDom">
  7. <a href='javascript:;'>
  8. <i :style="{ transform: item.transforms, webkitTransform: item.transforms}"></i>
  9. </a>
  10. </li>
  11. </template>
  12. </ul>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. data () {
  18. return {
  19. liParams: [],
  20. timer: null,
  21. duration: 10000 // 定义时间
  22. }
  23. },
  24. mounted () {
  25. this.startRedPacket()
  26. },
  27. methods: {
  28. /**
  29. * 开启动画
  30. */
  31. startRedPacket() {
  32. let win = document.documentElement.clientWidth || document.body.clientWidth
  33. let left = parseInt(Math.random() * (win - 50) + 0);
  34. let rotate = (parseInt(Math.random() * (45 - (-45)) - 45)) + "deg"; // 旋转角度
  35. let scales = (Math.random() * (12 - 8 + 1) + 8) * 0.1; // 图片尺寸
  36. let durTime = (Math.random() * (2.5 - 1.2 + 1) + 1.2) + 's'; // 时间 1.2和1.2这个数值保持一样
  37. console.log(durTime)
  38. this.liParams.push({left: left+'px', cls: 'move_1', transforms: 'rotate('+ rotate +') scale('+ scales +')', durTime: durTime})
  39. setTimeout( () => { // 多少时间结束
  40. clearTimeout(this.timer)
  41. return;
  42. }, this.duration)
  43. this.timer = setTimeout( () => {
  44. this.startRedPacket()
  45. },100)
  46. },
  47. /**
  48. * 回收dom节点
  49. */
  50. removeDom (e) {
  51. let target = e.currentTarget;
  52. document.querySelector('#red_packet').removeChild(target)
  53. }
  54. }
  55. }
  56. </script>
  57. <!-- Add "scoped" attribute to limit CSS to this component only -->
  58. <style scoped lang="scss">
  59. .ser_home {
  60. width: 100%;
  61. height: 100%;
  62. }
  63. .red_packet {
  64. display: block;
  65. position: relative;
  66. overflow: hidden;
  67. width: 100%;
  68. height: 100%;
  69. i {
  70. width: 48px;
  71. height: 69px;
  72. display: block;
  73. background: url('/hongbao.png') no-repeat;
  74. }
  75. li {
  76. position: absolute;
  77. animation: all 3s linear;
  78. top:-100px;
  79. z-index: 10;
  80. &.move_1 {
  81. -webkit-animation: aim_move 5s linear 1 forwards;
  82. animation: aim_move 5s linear 1 forwards;
  83. }
  84. }
  85. a {
  86. display: block;
  87. }
  88. }
  89. @keyframes aim_move {
  90. 0% {
  91. -webkit-transform: translateY(0);
  92. transform: translateY(0);
  93. }
  94. 100% {
  95. -webkit-transform: translateY(120vh);
  96. transform: translateY(120vh);
  97. }
  98. }
  99. </style>

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行