1. 程式人生 > 其它 >微信小程式中用swiper標籤時內容顯示不全解決方法:

微信小程式中用swiper標籤時內容顯示不全解決方法:

技術標籤:小程式

微信小程式中用swiper標籤時內容顯示不全解決方法:
第一,先在swiper標籤中的view標籤改成scroll-view標籤。(不然後面重新獲得長度也不能拉到底部顯示全部內容。當然,內容較少的頁面就可以不用改)

<swiper current="{{currentTab}}" >
    <swiper-item>
      <scroll-view scroll-y="{{true}}" >
        <!--大堆內容-->
      </scroll-view
>
</swiper-item> <swiper-item> <view class="litleWord"> <!--很少內容,不用改標籤--> </view> </swiper-item> </swiper>

第二步,動態獲取視窗高度應用到swiper和所有的scroll-view標籤。事實上,內容顯示不全就是因為獲取的高度過小。

<!--主檔案html中程式碼:-->
<swiper current="{{currentTab}}"
style="height:{{navHeight}}px">
<swiper-item> <scroll-view scroll-y="{{true}}" style="height:{{navHeight}}px"> <!--大堆內容--> </scroll-view> </swiper-item> <swiper-item> <view class="litleWord"
>
<!--很少內容,不用改標籤--> </view> </swiper-item> </swiper>
/*主檔案js程式碼*/
onLoad: function () {
    this.setData({
      navHeight:app.globalData.navHeight,
    })
},
/*app.js中的程式碼*/
onLaunch: function () {
    wx.getSystemInfo({
      success: res => {
        this.globalData.navHeight = res.windowHeight; //動態獲得視窗高度。當然,別忘了要在app.js中設定全域性變數navHeight,以及上面主檔案的js檔案也是同理。
        console.log(this.globalData.navHeight, 'this.globalData.navHeight')
      }, fail(err) {
        console.log(err);
      }
    })

  },

然後就可以發現顯示正常了~
若仍顯示不全,則試著在主檔案的js檔案中的onload函式裡把navHeight的值再自加100px。
程式碼如上,歡迎討論。