当前位置:   article > 正文

微信小程序实现红包雨效果_uniapp实现红包雨

uniapp实现红包雨

先看效果图

实现效果:

  1. 每个红包的image大小不一样
  2. 每个红包的旋转角度不一样
  3. 每个红包的下落速度不一样
  4. 每个红包的位置不一样

如何实现?

  • 在页面上我们用wx:for函数遍历js中的红包数组,如下:
  1. <view class='content'>
  2. <view class="hongbaoView" >
  3. <block wx:for="{{hongbaoArr}}" wx:for-item="item">
  4. <image style="transform:rotate({{item.jiaodu}}deg);margin-left:{{item.marginleft}}rpx;width:{{item.width}}rpx;height:{{item.height}}rpx;animation: dropdown {{item.animation}}s;animation-fill-mode:forwards;" class="hongbao" src='../images/红包.png' data-id='{{item.id}}' id="hongbao_{{item.id}}" bindtap='qianghongbao'></image>
  5. </block>
  6. </view>
  7. </view>

在image的style中,有几个参数是随机数,其中

  1. transform:rotate({{item.jiaodu}}deg);该参数为红包的旋转角度;
  2. margin-left:{{item.marginleft}}rpx;该参数为左边距,即红包的位置;

  3. width:{{item.width}}rpx;height:{{item.height}}rpx;这两个参数是红包的大小,由于红包的图片是32*32的,所以在JS中width=height,这样能保证红包图片不会变形;

  4. animation: dropdown {{item.animation}}s;该参数为红包下路的时间,下落时间不一样就能达到下落速度不一样的效果;

  • 在JS中我们需要随机生成红包,并push到红包数组中,完整JS如下:
  1. // pages/news/news.js
  2. Page({
  3. data: {
  4. hongbaoArr:null
  5. },
  6. /**
  7. * 生命周期函数--监听页面加载
  8. */
  9. onLoad: function (options) {
  10. this.getHongbao(1);
  11. },
  12. getHongbao:function(i){
  13. var shijian = Math.floor(Math.random() * (200-100)+100);
  14. var jiaodu = Math.ceil(Math.random() * 30)-15;
  15. var marginLeft = Math.ceil(Math.random() * 690);
  16. var width = Math.floor(Math.random() * (100 - 60 + 1) + 60);
  17. var animation = Math.floor(Math.random() * (6-3)+3)
  18. console.log(marginLeft);
  19. var hongbao = {
  20. id: i,
  21. jiaodu: jiaodu,
  22. marginleft: marginLeft,
  23. width: width,
  24. height: width,
  25. animation: animation
  26. }
  27. var hongbaoA = this.data.hongbaoArr;
  28. if (hongbaoA == null){
  29. hongbaoA = new Array();
  30. }
  31. hongbaoA.push(hongbao);
  32. this.setData({
  33. hongbaoArr: hongbaoA
  34. })
  35. var that = this;
  36. if(i <= 100){
  37. var timerTem =setTimeout(function () {
  38. i = i+ 1;
  39. console.log(i);
  40. that.getHongbao(i)
  41. }, shijian)
  42. }
  43. },
  44. qianghongbao:function(e){
  45. var id = e.currentTarget.dataset['id'];
  46. var query = wx.createSelectorQuery();
  47. var hongbao = query.select("#"+id);
  48. console.log(hongbao);
  49. hongbao.classList.remove('animate');
  50. }
  51. })

下面讲解一下JS

  1. data中的hongbaoArr即为我们的红包数组;
  2. 在页面加载时调用getHongbao函数来获取红包;
  3. 在getHongbao中先获取几个随机数,其中var shijian = Math.floor(Math.random() * (200-100)+100);这个为每生成一个红包需要等待shijian毫秒的时间才会生成下一个红包;
  4. if(i <= 100){

    var timerTem =setTimeout(function () {

    i = i+ 1;

    console.log(i);

    that.getHongbao(i)

    }, shijian)

    }该段代码控制生成100个红包,采用递归的方式

  5. qianghongbao这个函数请忽略

  • CSS中我们只需写一些很简单样式就好
  1. .hongbao{
  2. position: absolute;
  3. }
  4. @keyframes dropdown {
  5. from{margin-top: -50rpx;}
  6. to{margin-top: 1200rpx;}
  7. }

比较简单就不赘述了。

打开红包效果见微信小程序打开红包效果

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