页面优化
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) => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<wxs src="./utils.wxs" module="Utils" />
|
||||
<t-image
|
||||
t-class="J-image"
|
||||
src="{{Utils.getSrc({src, thumbWidth: thumbWidth || 0, thumbHeight: thumbHeight || 0, systemInfo, webp, mode})}}"
|
||||
class="J-image"
|
||||
src="{{Utils.getSrc({src, thumbWidth: thumbWidth || 0, thumbHeight: thumbHeight || 0, screenWidth, pixelRatio, webp, mode})}}"
|
||||
t-class="t-class"
|
||||
t-class-load="t-class-load"
|
||||
mode="{{ mode }}"
|
||||
|
||||
@@ -119,13 +119,11 @@ function getSrc(options) {
|
||||
return imageMogr(options.src, {
|
||||
width:
|
||||
options.mode !== 'heightFix'
|
||||
? rpx2px(options.thumbWidth, options.systemInfo.screenWidth) *
|
||||
options.systemInfo.pixelRatio
|
||||
? rpx2px(options.thumbWidth, options.screenWidth) * (options.pixelRatio || 2)
|
||||
: null,
|
||||
height:
|
||||
options.mode !== 'widthFix'
|
||||
? rpx2px(options.thumbHeight, options.systemInfo.screenWidth) *
|
||||
options.systemInfo.pixelRatio
|
||||
? rpx2px(options.thumbHeight, options.screenWidth) * (options.pixelRatio || 2)
|
||||
: null,
|
||||
format: options.webp ? 'webp' : null,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user