<template>
<view class="banner" style="line-height: 0;">
<swiper v-else :autoplay="true" :indicator-dots="true" :interval="interval" :style="bannerStyle">
<swiper-item v-for="item in banners" :key="item">
<image :src="item" style="width:750rpx;line-height:normal;" mode="widthFix" @load="handleLoaded"></image>
</swiper-item>
</swiper>
</view>
</template>
<script>
export default {
data() {
return {
bannerHeight: 30,
banners:['图片路径']
};
},
computed:{
bannerStyle() {
return `height:${Math.floor(this.bannerHeight)}px`;
}
},
methods: {
handleLoaded(e) {
const { width, height } = e.mp.detail;
console.log('###图片宽高:', width, height);
let imgHeight = uni.upx2px(height);
const screenWidth = uni.upx2px(750);
if (imgHeight>this.bannerHeight) {
if(width<=height){
if (imgHeight<screenWidth)
imgHeight += (screenWidth-imgHeight)/screenWidth*screenWidth;
else if (imgHeight>screenWidth)
imgHeight = screenWidth;
}
this.bannerHeight = imgHeight;
}
},
}
}
</script>
- 发布:2022-06-23 10:53
- 更新:2022-06-24 10:53
- 阅读:441
产品分类: uniapp/H5
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: Windows 10 企业版 21H2
HBuilderX类型: 正式
HBuilderX版本号: 3.4.15
浏览器平台: Chrome
浏览器版本: 102
项目创建方式: HBuilderX
示例代码:
操作步骤:
分别运行程序到H5模拟器和小程序模拟器获取的结果是不一样的
分别运行程序到H5模拟器和小程序模拟器获取的结果是不一样的
预期结果:
通过 e.mp.detail应该获取到的都是图片真实宽高,或者小程序和H5结果应该是一致的
通过 e.mp.detail应该获取到的都是图片真实宽高,或者小程序和H5结果应该是一致的
实际结果:
真实图片为:750*400像素,H5获取的结果是缩放后的,小程序获取到的结果是真实宽高
真实图片为:750*400像素,H5获取的结果是缩放后的,小程序获取到的结果是真实宽高
bug描述:
今天突然发现小程序的Banner高度在H5和小程序表现不一致,测试后发现是image组件的load事件中通过e.mp.detail获取图片宽高信息H5和小程序获取的结果不一致,小程序获取的是图片的真实宽高,H5获取的是物理宽高,之前是没有这个问题的,麻烦DCloud的工程狮们看下是不是BUG。