页面优化
This commit is contained in:
@@ -6,7 +6,25 @@
|
||||
* @Description: webp-image组件对t-image包裹了一层,主要实现图片裁剪、webp压缩功能
|
||||
* @FilePath: /tdesign-miniprogram-starter/components/webp-image/index.js
|
||||
*/
|
||||
const systemInfo = wx.getSystemInfoSync();
|
||||
let screenWidth = 375;
|
||||
let pixelRatio = 2;
|
||||
let screenReady = false;
|
||||
|
||||
function ensureScreen() {
|
||||
if (screenReady) {
|
||||
return;
|
||||
}
|
||||
screenReady = true;
|
||||
try {
|
||||
const info = wx.getWindowInfo();
|
||||
screenWidth = info.screenWidth || info.windowWidth || 375;
|
||||
pixelRatio = info.pixelRatio || 2;
|
||||
} catch (err) {
|
||||
screenWidth = 375;
|
||||
pixelRatio = 2;
|
||||
}
|
||||
}
|
||||
|
||||
Component({
|
||||
externalClasses: ['t-class', 't-class-load'],
|
||||
properties: {
|
||||
@@ -42,9 +60,14 @@ Component({
|
||||
data: {
|
||||
thumbHeight: 375,
|
||||
thumbWidth: 375,
|
||||
systemInfo,
|
||||
screenWidth: 375,
|
||||
pixelRatio: 2,
|
||||
},
|
||||
lifetimes: {
|
||||
attached() {
|
||||
ensureScreen();
|
||||
this.setData({ screenWidth, pixelRatio });
|
||||
},
|
||||
ready() {
|
||||
const { mode } = this.properties;
|
||||
// 获取容器的真实宽高,设置图片的裁剪宽度
|
||||
@@ -66,7 +89,8 @@ Component({
|
||||
},
|
||||
methods: {
|
||||
px2rpx(px) {
|
||||
return (750 / (systemInfo.screenWidth || 375)) * px;
|
||||
ensureScreen();
|
||||
return (750 / (screenWidth || 375)) * px;
|
||||
},
|
||||
getRect(selector) {
|
||||
return new Promise((resolve) => {
|
||||
|
||||
Reference in New Issue
Block a user