小程序-swiper高度自适应,改变默认高度,嵌套scroll-view【转载】
发布于 4 年前 作者 xia26 4710 次浏览 来自 分享

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

    })

  },

这就可以完美解决了。

PS:上述经本人亲自测试,是成功的!为便于查找,故转载存档,原址(https://blog.csdn.net/u012054869/article/details/88018966),感谢原作者的倾力奉献!

回到顶部