微信小程序使用animation动画实现消息从左向右滚动

<view id="notice" style="overflow:hidden;position: relative;height:50rpx;margin:0 50rpx;">
  <view id="notice-txt" style="white-space: nowrap;position: absolute;" animation="{{animationData}}">足协杯战线连续第2年上演广州德比战足协杯战线连续第2年上演广州德比战足协杯战线连续第2年上演广州德比战</view>
</view>
<view bindtap="startAnimation">移动</view>
  initAnimation(){
    wx.nextTick(() => {
      //获取外部容器和滚动内容的宽度
      const query = wx.createSelectorQuery();
      query.select('#notice').boundingClientRect(rect=>{
        this.data.wrapWidth = parseInt(rect.width);
      }).exec();
      query.select('#notice-txt').boundingClientRect(rect=>{
        this.data.contentWidth = parseInt(rect.width);
      }).exec();
    })
  },
  startAnimation(){
    const {wrapWidth,contentWidth} = this.data;
    let duration = contentWidth / 20 * 200; //每次滚动的时间

    let timer = null;
    timer&&clearTimeout(timer);
    //设置滚动内容在右侧的初始位置
    this.setData({
      animationData:wx.createAnimation({duration: 0}).translateX(wrapWidth).step().export()
    });
    //设置滚动内容最终的位置
    setTimeout(()=>{
      this.setData({
        animationData:wx.createAnimation({duration: duration}).translateX(-contentWidth).step().export()
      });
    },20)
    //滚动一次完成后开始下一次的滚动
    timer = setTimeout(()=>{
      this.startAnimation();
    },duration+1000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.initAnimation();
  },

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值