项目中遇到,研究一下,特此记录,不喜勿喷!
此方法适用于swiper图片宽度铺满屏幕,可根据需求自定义调整
原理:获取图片高度,根据图片宽高比例,取得swiper在宽度100vw情况下的高度,实现swiper高度根据内容图片高度适应
template:
...
<swiper class="swiper" :style="`height:${swiperheight}`">
<swiper-item class="swiperitem" v-for="(item,index) in imgList" :key="index" >
</swiper-item>
</swiper>
...
js
export default {
data() {
return {
swiperheight:"0px",
imgList:[...]
}
},
onLoad() {
this.getswiperImageInfo()
},
methods: {
getswiperImageInfo(src){
uni.getImageInfo({
src: this.imgList[0].img,
success: (res) => {
console.log(res)
let endwidth = res.width/res.height
this.swiperheight = 100/endwidth+"vw"
},
fail: (err) => {
console.log(err)
}
});
}
}
}
css
.swiper{
width: 100vw;
}
0 个评论
要回复文章请先登录或注册