小程序使用动画时的 px 单位 转 rpx的方法

借助API

wx.getSystemInfoSync();
通过API可获取的值:


// 在 iPhone6 下运行:
 
var systemInfo = wx.getSystemInfoSync();
console.log(systemInfo.windowWidth); // 输出 375(单位 px)
 
// 在 iPhone6 Plus 下:
var systemInfo = wx.getSystemInfoSync();
console.log(systemInfo.windowWidth); // 输出 414 (单位 px)

px与rpx之间转换的公式

var px = rpx / 750 * wx.getSystemInfoSync().windowWidth;

动画中如何使用:

//假设我想向右平移300rpx,动画代码如下:
this.animation.translateX(300 / 750 * systemInfo.windowWidth).step()

这样在所有机型上都可以进行适配。🐣

posted @ 2020-06-05 10:52  Homegu  阅读(291)  评论(0编辑  收藏  举报