当前位置:   article > 正文

小程序红包雨_uniapp红包雨

uniapp红包雨

效果图

主要逻辑代码:

  1. //建立临时红包列表
  2. var packetList = [];
  3. //建立临时红包图片数组
  4. var srcList = ["../images/hogbao.png", "../images/hogbao2.png"];
  5. //生成初始化红包
  6. for (var i = 0; i < that.data.packetNum; i++) {
  7. // 生成随机位置(水平位置)
  8. var left = Math.random() * that.data.windowWidth - 20;
  9. // 优化位置,防止红包越界现象,保证每个红包都在屏幕之内
  10. if (left < 0) {
  11. left += 20;
  12. } else if (left > that.data.windowWidth) {
  13. left -= 20;
  14. }
  15. // 建立临时单个红包
  16. var packet = {
  17. src: srcList[Math.ceil(Math.random() * 2) - 1],
  18. top: -30,
  19. left: left,
  20. speed: Math.random() * 2500 + 3000 //生成随机掉落时间,保证每个掉落时间保持在3秒到5.5秒之间
  21. }
  22. // 将单个红包装入临时红包列表
  23. packetList.push(packet);
  24. // 将生成的临时红包列表更新至页面数据,页面内进行渲染
  25. that.setData({
  26. packetList: packetList
  27. })
  28. }
  29. // 初始化动画执行当前索引
  30. var tempIndex = 0;
  31. // 开始定时器,每隔1秒掉落一次红包
  32. that.data.showInter = setInterval(function () {
  33. // 生成当前掉落红包的个数,1-3
  34. var showNum = Math.ceil(Math.random() * 3);
  35. // 防止数组越界
  36. if (tempIndex * showNum >= that.data.packetNum) {
  37. // 如果所有预生成的红包已经掉落完,清除定时器
  38. clearInterval(that.data.showInter);
  39. } else {
  40. switch (showNum) {
  41. case 1:
  42. //设置临时红包列表当前索引下的top值,此处top值为动画运动的最终top
  43. packetList[tempIndex].top = that.data.windowHeigh;
  44. // 当前次掉落几个红包,索引值就加几
  45. tempIndex += 1;
  46. break;
  47. case 2:
  48. packetList[tempIndex].top = that.data.windowHeigh;
  49. packetList[tempIndex + 1].top = that.data.windowHeigh;
  50. tempIndex += 2;
  51. break;
  52. case 3:
  53. packetList[tempIndex].top = that.data.windowHeigh;
  54. packetList[tempIndex + 1].top = that.data.windowHeigh;
  55. packetList[tempIndex + 2].top = that.data.windowHeigh;
  56. tempIndex += 3;
  57. break;
  58. default:
  59. console.log();
  60. }
  61. // 更新红包列表数据
  62. that.setData({
  63. packetList: packetList
  64. })
  65. }
  66. }, 1000)
  67. console.log("数据" + that.data.showInter)
  68. },

CSS代码:

  1. <view wx:for="{{packetList}}" wx:for-index="index" wx:for-item="items">
  2. <image class="red-packet" src="{{items.src}}" style="position:fixed;top:{{items.top}}px;left:{{items.left}}px;-webkit-transition:{{items.speed}}ms linear 0ms;transition:{{items.speed}}ms linear 0ms" bindtap='tapImages' >
  3. </image>
  4. </view>

WXML代码:

  1. .red-packet{
  2. width: 20px;
  3. height: 25px;
  4. z-index: 100;
  5. transition-property:transform,top;
  6. transform-origin:50% 50% 0;
  7. -webkit-transition-property:transform,top;
  8. -webkit-transform-origin:50% 50% 0;
  9. }

JS代码

  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. windowWidth: "",//窗口宽度
  7. windowHeigh: "",//窗口高度
  8. packetList: [{}],//红包队列
  9. packetNum: 200,//总共红包的数量
  10. showInter: ''// 循环动画定时器
  11. },
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. onLoad: function (options) {
  16. var that = this;
  17. // 获取手机屏幕宽高
  18. wx.getSystemInfo({
  19. success: function (res) {
  20. that.setData({
  21. windowWidth: res.windowWidth,
  22. windowHeigh: res.windowHeight,
  23. top: res.windowHeight - 100 //设置红包初始位置
  24. })
  25. }
  26. })
  27. //建立临时红包列表
  28. var packetList = [];
  29. //建立临时红包图片数组
  30. var srcList = ["../images/hogbao.png", "../images/hogbao2.png"];
  31. //生成初始化红包
  32. for (var i = 0; i < that.data.packetNum; i++) {
  33. // 生成随机位置(水平位置)
  34. var left = Math.random() * that.data.windowWidth - 20;
  35. // 优化位置,防止红包越界现象,保证每个红包都在屏幕之内
  36. if (left < 0) {
  37. left += 20;
  38. } else if (left > that.data.windowWidth) {
  39. left -= 20;
  40. }
  41. // 建立临时单个红包
  42. var packet = {
  43. src: srcList[Math.ceil(Math.random() * 2) - 1],
  44. top: -30,
  45. left: left,
  46. speed: Math.random() * 2500 + 3000 //生成随机掉落时间,保证每个掉落时间保持在3秒到5.5秒之间
  47. }
  48. // 将单个红包装入临时红包列表
  49. packetList.push(packet);
  50. // 将生成的临时红包列表更新至页面数据,页面内进行渲染
  51. that.setData({
  52. packetList: packetList
  53. })
  54. }
  55. // 初始化动画执行当前索引
  56. var tempIndex = 0;
  57. // 开始定时器,每隔1秒掉落一次红包
  58. that.data.showInter = setInterval(function () {
  59. // 生成当前掉落红包的个数,1-3
  60. var showNum = Math.ceil(Math.random() * 3);
  61. // 防止数组越界
  62. if (tempIndex * showNum >= that.data.packetNum) {
  63. // 如果所有预生成的红包已经掉落完,清除定时器
  64. clearInterval(that.data.showInter);
  65. } else {
  66. switch (showNum) {
  67. case 1:
  68. //设置临时红包列表当前索引下的top值,此处top值为动画运动的最终top
  69. packetList[tempIndex].top = that.data.windowHeigh;
  70. // 当前次掉落几个红包,索引值就加几
  71. tempIndex += 1;
  72. break;
  73. case 2:
  74. packetList[tempIndex].top = that.data.windowHeigh;
  75. packetList[tempIndex + 1].top = that.data.windowHeigh;
  76. tempIndex += 2;
  77. break;
  78. case 3:
  79. packetList[tempIndex].top = that.data.windowHeigh;
  80. packetList[tempIndex + 1].top = that.data.windowHeigh;
  81. packetList[tempIndex + 2].top = that.data.windowHeigh;
  82. tempIndex += 3;
  83. break;
  84. default:
  85. console.log();
  86. }
  87. // 更新红包列表数据
  88. that.setData({
  89. packetList: packetList
  90. })
  91. }
  92. }, 1000)
  93. console.log("数据" + that.data.showInter)
  94. },
  95. //点击图片
  96. tapImages: function (event){
  97. var score = Math.ceil(Math.random() * 100);
  98. wx.showToast({
  99. title: "得分"+score,
  100. icon: 'succes',
  101. duration: 1000,
  102. mask: true
  103. })
  104. }
  105. })

直接使用

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