小程序使用 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

相關文章
相關標籤/搜索