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

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

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

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

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

posted @ 2019-03-27 16:45  电子猫  阅读(6183)  评论(0编辑  收藏  举报
博客已经出生了585天12小时9分18秒