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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hjh15827475896/article/details/85320283

1、需要借助的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)

2、px与rpx之间转换的公式:px = rpx / 750 * wx.getSystemInfoSync().windowWidth;

动画中如何使用:

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

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

猜你喜欢

转载自blog.csdn.net/hjh15827475896/article/details/85320283