小程序swiper(tab)高度自适应

swiper高度问题一直困扰我。今天终于有时间来解决一下。因为他的高度不能固定死,写死其他数据展示不完全,不写或者100%,auto都不行。翻了一堆资料也查了很多,最后总结一下。
1.很多人都说用一种方法。就是高度*数量,也就是所说的获取数据数组长度,根据数据长度来动态改变每页的长度,因为字号啊什么的在各个手机显示不一定都相同,总感觉不是解决问题的最佳方法。
2.使用Swiper+scroll-view
先设置swiper高度

<swiper style="height: {{clientHeight?clientHeight+'px':'auto'}}" class='videoSwiper'  current="{{currentTab}}"  duration="200" bindchange="swiperchange"></swiper>

在swiper-item中嵌套一个scroll-view

<swiper-item >
     <scroll-view scroll-y="{{true}}" style="height: {{clientHeight?clientHeight+'px':'auto'}}" bindscrolltolower="scrollbot">
     </scroll-view>
</swiper-item >

在js中获取设备可视窗口高度(我是写在onload里的)

onLoad:function(){
        var that = this
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    clientHeight: res.windowHeight
                });
            }
        })
    }

切换的js

swiperchange: function (e) {
        var that = this
        console.log(e.detail.current)
        that.setData({
            'currentTab': e.detail.current
        })
    },

这就可以完美解决了。
在这里插入图片描述
最后和朋友创了个南京前端交流群,有大佬有菜鸡,有妹纸。大家可以一起讨论技术共同进步。群号811258693

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值