当前位置:首页 > 电子 > 微信小程序自定义导航栏高度获取出错windowHeight值不一致

微信小程序自定义导航栏高度获取出错windowHeight值不一致

具体的实现思路是,用wx.getSystemInfo获取可使用的屏幕高度减去头部的固定高度,作为滚动层scroll-view的使用高度;
核心代码如下

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



然而在实际使用过程中发现,多个页面中每个页面调用wx.getSystemInfo获取可使用的屏幕高度的值在不同真机的出现了值不一致的问题,这也就导致了滚动视图层显示效果不佳;
解决思路是通过设置全局变量的方式;具体实现方式如下;
app.js


  onLaunch: function () {
    wx.getSystemInfo({
      success: function (res) {
        that.globalData.windowHeight = res.windowHeight
      }
    })
  }
  ,
  globalData: {
    windowHeight:null
  }


需要获取windowHeight的页面js
如index.js

const app = getApp()
onReady:function(){
    var tbodyHeight = app.globalData.windowHeight - 90; //90为头部固定高度 
    that.setData({
        tbodyHeight: tbodyHeight.toFixed(0)
    })
 }


好了,以上为我对微信小程序windowHeight每个页面值不一致的解决方案。


除特别注明外,本站所有文章均为视觉电子科技原创,转载请注明出处来自https://feelsight.cn/post/138.html

赞 (0

发表评论

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

分享:

支付宝

微信