小程序-动画,距离移动端适配方法

小程序动画,距离移动端适配方法

问题:

由于小程序动画如下:

  that.animation.translateY(-640 ).step()

距离没有单位,据实测是手机的分辨率,物理像素乘以dpr,导致在不同手机上移动距离不相同


解:

使用小程序的获取系统信息方法wx.getSystemInfo获取windowWidth(窗口宽度)和pixelRatio (设备像素比)计算出当前rpx倍数,这里使用的是750是iphone6的宽度,是2倍计算起来比较方便。代码如下

var that=this;//小程序对象建议that一下,this可能在后面使用中被替换
wx.getSystemInfo({
      success: function (res) {
        let rpx = 1 * (res.windowWidth * res.pixelRatio) / (750 * res.pixelRatio);
        that.setData({
          rpx: rpx//添加到小程序全局data里面
        })
      }
    });
    //使用时
    that.animation.translateY(-640*that.data.rpx).step();

下面附上我动画的代码可以看一下
wxml代码:

<view class='payfor' animation="{{animation}}"style=''>
这个是动画
</view>
<button class='' bindtap='move'>move</button>

js代码:

data:{
rpx: 0,
animation: '',
moveKey:true,
},
 onLoad(options) {
    var that =this;
     wx.getSystemInfo({
      success: function (res) {
        let rpx = 1 * (res.windowWidth * res.pixelRatio) / (750 * res.pixelRatio);
        that.setData({
          rpx: rpx
        })
      }
    })
    //实例化一个动画
    this.animation = wx.createAnimation({
      // 动画持续时间,单位ms,默认值 400
      duration: 300,
      timingFunction: 'ease-out',
      // 延迟多长时间开始
      delay: 0,
      transformOrigin: 'left top 0',
      success: function (res) {
        // console.log(res)
      }
    })
},
  doAnimation(key) {//动画上部

    var that = this;
    if (key == true) {
      setTimeout(function () {
        that.animation.translateY(-640 * that.data.rpx).step()
        that.setData({
          //输出动画
          animation: that.animation.export(),
          close:true
        })
      }, 0)

    } else {
      setTimeout(function () {
        that.animation.translateY(0 * that.data.rpx).step()
        that.setData({
          //输出动画
          animation: that.animation.export()
        })
      }, 0)
    },
    move(){
    var that= this;
    var moveKey=that.data.moveKey;
    that.doAnimation(!key)
    that.setData({
    moveKey:!moveKey
    })
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值