微信小程序雪花背景(动态)

效果:效果为动态

 一:wxml片段

<scroll-view scroll-y="true">
  <!-- 页面内容 -->
</scroll-view>
<view>
  <image wx:for="{{snows}}" wx:key="index" src="/img/snow.png" style="left:{{snows[index]}}rpx" animation="{{animation[index]}}" class='snow' data-index='{{index}}' />
</view>

 二:JS片段

// pages/snows/snows.js
var i = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    snows: [],
    animation: [],
    dateTime: "",
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.initSnow();
    this.data.snows = [];
    this.data.animation = [];
    let j = 50
    while (j--)
      this.data.snows.push(Math.floor(Math.random() * 700))
    this.setData({
      snows: this.data.snows
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    clearTimeout(this.data.dateTime)
    this.setData({
      snows: [],
      animation: []
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearTimeout(this.data.dateTime)
    this.setData({
      snows: [],
      animation: []
    })
  },
  initSnow: function () {
    setTimeout(function () {
      let animation = wx.createAnimation({})
      animation.translateY(804).opacity(1).step({
        duration: 4000
      })
      animation.translateY(0).opacity(1).step({
        duration: 0
      })
      this.setData({
        ['snows[' + i + ']']: Math.floor(Math.random() * 700),
        ['animation[' + i + ']']: animation.export()
      })
      i++;
      if (i == 50)
        i = 0
    }.bind(this), 500)
    var dateTime = setTimeout(function () {
      this.initSnow()
    }.bind(this), 100)
    this.setData({
      dateTime,
    })
  },

})

三:WXSS片段

page {
  background: linear-gradient(180deg, #cc66a2 0%, #B8DBF0 100%);
  background-attachment: fixed;
  background-size: cover;
  background-position: center top;
  height: 100%;
}

.snow {
  width: fit-content;
  position: absolute;
  top: -100rpx;
  width: 30rpx;
  height: 27rpx;
  z-index: 11;
}

.snow:nth-child(2n+2) {
  width: 35rpx;
  height: 33rpx;
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值