小程序使用 rpx 单位 转 px的方法(用于动画、canvas画图)

一、须要借助的API:wx.getSystemInfoSync();

经过API可获取的值:动画

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

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

1 // 假设设计图中按钮向右偏移了300rpx,动画代码以下:
2 this.animation.translateX(300 / 750 * systemInfo.windowWidth).step()

这样在全部机型上均可以进行适配。this

相关文章
相关标签/搜索