评论

小程序-swiper高度自适应,改变默认高度,嵌套scroll-view【转载】

小程序-swiper高度自适应,改变默认高度,在swiper-item中嵌套一个scroll-view。

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),感谢原作者的倾力奉献!

最后一次编辑于  2020-11-01  
点赞 0
收藏
评论

1 个评论

  • 可乐
    可乐
    2021-12-03

    如果页面一半用了这个,上半部分是别的内容,那我页面上下滚动的时候只有scroll-view部分滚动,而scroll-view内容区域以上部分不会跟随页面滚动

    2021-12-03
    赞同
    回复 1
    • 清风明月
      清风明月
      2022-12-04
      这种怎么解决呢?
      2022-12-04
      回复
登录 后发表内容