1***@qq.com
1***@qq.com
  • 发布:2019-01-24 19:44
  • 更新:2019-10-16 17:24
  • 阅读:2784

swiper高度只能设置固定值,如何自适应

分类:uni-app
2019-01-24 19:44 负责人:无 分享
已邀请:
Trust

Trust - 少说废话

JS 计算后设置 style

1***@qq.com

1***@qq.com

我计算后在浏览器正常,真机不行

n***@163.com

n***@163.com

还有人吗,挖个坟。。。

swiper编译后下面有个div 的class: uni-swiper .uni-swiper-wrapper 设置了position:relative;和height:100%;
导致该div会对照父布局的height,也就是150px;

我是这样解决的:
//自动填充满父布局
swiper {
display:flex;
flex: 1 1 auto;
}
//使用first-child覆盖uni-swiper .uni-swiper-wrapper,将position覆盖为position: absolute;
//或将height:100%干掉也行
swiper :first-child{
position: absolute;
}

酒菜儿

酒菜儿

亲测可用

<template>  
    <view>  
        {{items[current].height}}  

        <swiper class="swiper" @change="change" :style="{height: items[current].height + 'px'}">  
            <swiper-item>  
                <view class="swiper-item">  
                    <view class="bgwhite mt16 p16" v-for="d in 4" :key="d">{{d}}</view>  
                </view>  
            </swiper-item>  
            <swiper-item>  
                <view class="swiper-item">  
                    <view class="bgwhite mt16 p16" v-for="d in 20" :key="d">{{d}}</view>  
                </view>  
            </swiper-item>  
            <swiper-item>  
                <view class="swiper-item">  
                    <view class="bgwhite mt16 p16" v-for="d in 10" :key="d">{{d}}</view>  
                </view>  
            </swiper-item>  
        </swiper>  

    </view>  
</template>  

<script>  
    export default {  
        data() {  
            return {  
                current: 0,  
                items: []  
            }  
        },  

        mounted() {  
            uni.createSelectorQuery().selectAll('.swiper-item').boundingClientRect(data => this.items = data).exec()  
        },  

        methods: {  
            change(e) {  
                this.current = e.detail.current  
            }  
        }  
    }  
</script>  

该问题目前已经被锁定, 无法添加新回复