leeooo
leeooo
  • 发布:2021-09-19 12:52
  • 更新:2021-09-19 12:52
  • 阅读:3471

【简单原理】满足swiper组件的高度自适应,随内容<图片>高度变化而变化

分类:uni-app

项目中遇到,研究一下,特此记录,不喜勿喷!

此方法适用于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 关注 分享

要回复文章请先登录注册