小程序红包雨

效果图

主要逻辑代码:

//建立临时红包列表
    var packetList = [];
    //建立临时红包图片数组
    var srcList = ["../images/hogbao.png", "../images/hogbao2.png"];
    //生成初始化红包
    for (var i = 0; i < that.data.packetNum; i++) {
      // 生成随机位置(水平位置)
      var left = Math.random() * that.data.windowWidth - 20;
      // 优化位置,防止红包越界现象,保证每个红包都在屏幕之内
      if (left < 0) {
        left += 20;
      } else if (left > that.data.windowWidth) {
        left -= 20;
      }
      // 建立临时单个红包
      var packet = {
        src: srcList[Math.ceil(Math.random() * 2) - 1],
        top: -30,
        left: left,
        speed: Math.random() * 2500 + 3000     //生成随机掉落时间,保证每个掉落时间保持在3秒到5.5秒之间
      }
      // 将单个红包装入临时红包列表
      packetList.push(packet);
      // 将生成的临时红包列表更新至页面数据,页面内进行渲染
      that.setData({
        packetList: packetList
      })
    }

    // 初始化动画执行当前索引
    var tempIndex = 0;
    // 开始定时器,每隔1秒掉落一次红包
    that.data.showInter = setInterval(function () {
      // 生成当前掉落红包的个数,1-3个
      var showNum = Math.ceil(Math.random() * 3);
      // 防止数组越界
      if (tempIndex * showNum >= that.data.packetNum) {
        // 如果所有预生成的红包已经掉落完,清除定时器
        clearInterval(that.data.showInter);
      } else {
        switch (showNum) {
          case 1:
            //设置临时红包列表当前索引下的top值,此处top值为动画运动的最终top值 
            packetList[tempIndex].top = that.data.windowHeigh;
            // 当前次掉落几个红包,索引值就加几
            tempIndex += 1;
            break;
          case 2:
            packetList[tempIndex].top = that.data.windowHeigh;
            packetList[tempIndex + 1].top = that.data.windowHeigh;
            tempIndex += 2;
            break;
          case 3:
            packetList[tempIndex].top = that.data.windowHeigh;
            packetList[tempIndex + 1].top = that.data.windowHeigh;
            packetList[tempIndex + 2].top = that.data.windowHeigh;
            tempIndex += 3;
            break;
          default:
            console.log();
        }
        // 更新红包列表数据
        that.setData({
          packetList: packetList
        })
      }
    }, 1000)

    console.log("数据" + that.data.showInter)
    
  },

CSS代码:

<view wx:for="{{packetList}}" wx:for-index="index" wx:for-item="items">
  <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' >
  </image>
</view>

WXML代码:

.red-packet{
  width: 20px;
  height: 25px;
  z-index: 100;
  transition-property:transform,top;
  transform-origin:50% 50% 0;
  -webkit-transition-property:transform,top;
  -webkit-transform-origin:50% 50% 0;
}

JS代码

Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowWidth: "",//窗口宽度
    windowHeigh: "",//窗口高度
    packetList: [{}],//红包队列
    packetNum: 200,//总共红包的数量
    showInter: ''//  循环动画定时器
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 获取手机屏幕宽高
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowWidth: res.windowWidth,
          windowHeigh: res.windowHeight,
          top: res.windowHeight - 100   //设置红包初始位置
        })
      }
    })
    //建立临时红包列表
    var packetList = [];
    //建立临时红包图片数组
    var srcList = ["../images/hogbao.png", "../images/hogbao2.png"];
    //生成初始化红包
    for (var i = 0; i < that.data.packetNum; i++) {
      // 生成随机位置(水平位置)
      var left = Math.random() * that.data.windowWidth - 20;
      // 优化位置,防止红包越界现象,保证每个红包都在屏幕之内
      if (left < 0) {
        left += 20;
      } else if (left > that.data.windowWidth) {
        left -= 20;
      }
      // 建立临时单个红包
      var packet = {
        src: srcList[Math.ceil(Math.random() * 2) - 1],
        top: -30,
        left: left,
        speed: Math.random() * 2500 + 3000     //生成随机掉落时间,保证每个掉落时间保持在3秒到5.5秒之间
      }
      // 将单个红包装入临时红包列表
      packetList.push(packet);
      // 将生成的临时红包列表更新至页面数据,页面内进行渲染
      that.setData({
        packetList: packetList
      })
    }

    // 初始化动画执行当前索引
    var tempIndex = 0;
    // 开始定时器,每隔1秒掉落一次红包
    that.data.showInter = setInterval(function () {
      // 生成当前掉落红包的个数,1-3个
      var showNum = Math.ceil(Math.random() * 3);
      // 防止数组越界
      if (tempIndex * showNum >= that.data.packetNum) {
        // 如果所有预生成的红包已经掉落完,清除定时器
        clearInterval(that.data.showInter);
      } else {
        switch (showNum) {
          case 1:
            //设置临时红包列表当前索引下的top值,此处top值为动画运动的最终top值 
            packetList[tempIndex].top = that.data.windowHeigh;
            // 当前次掉落几个红包,索引值就加几
            tempIndex += 1;
            break;
          case 2:
            packetList[tempIndex].top = that.data.windowHeigh;
            packetList[tempIndex + 1].top = that.data.windowHeigh;
            tempIndex += 2;
            break;
          case 3:
            packetList[tempIndex].top = that.data.windowHeigh;
            packetList[tempIndex + 1].top = that.data.windowHeigh;
            packetList[tempIndex + 2].top = that.data.windowHeigh;
            tempIndex += 3;
            break;
          default:
            console.log();
        }
        // 更新红包列表数据
        that.setData({
          packetList: packetList
        })
      }
    }, 1000)

    console.log("数据" + that.data.showInter)
    
  },

  //点击图片
  tapImages: function (event){

    var score = Math.ceil(Math.random() * 100);
    wx.showToast({
      title: "得分"+score,
      icon: 'succes',
      duration: 1000,
      mask: true
    })
  }
})

直接使用

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VX红包封面小程序源码是一种用于微信小程序的编程代码,用于实现在微信中发送红包封面的功能。这种红包封面小程序源码可以使用户在微信中发送红包,将红包以封面的形式展示给接收者。 这个小程序源码的实现主要有以下几个步骤: 首先,编写界面代码,包括设置红包封面的背景图片、封面上的文字内容以及显示接收者头像的部分。这个界面的编写需要使用小程序的开发工具,通过编写HTML、CSS和JavaScript代码实现。 然后,需要编写后台逻辑代码,包括红包金额的分配和发送功能。在用户发送红包时,后台逻辑代码根据设置的规则对红包金额进行分配,并将分配结果存储在服务器中。接收者打开红包封面时,后台逻辑代码会判断该用户是否有资格领取红包,并将红包金额显示在界面上。 最后,需要进行测试和发布。测试阶段主要是检查红包发送和接收的功能是否正常,并进行修复和优化。发布红包封面小程序源码时,需要将代码打包成小程序,并将小程序上传到微信开放平台进行审核。审核通过后,用户就可以在微信中搜索并使用这个红包封面小程序了。 总的来说,VX红包封面小程序源码是一个实现在微信中发送红包封面的功能的代码,它包括界面代码和后台逻辑代码,通过这些代码的编写和测试,用户可以在微信中发送和接收红包,并以封面的形式展示给接收者。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值