小程序 wx.getSystemInfoSync 獲取 windowHeight 問題


windowHeight 概念

可使用窗口高度,即:屏幕高度(screenHeight) - 導航(tabbar)高度

存在問題

安卓設備下獲取 windowHeight 不能准確得到對應的高度,總是拿到屏幕高度

原因

1. 同步接口 wx.getSystemInfoSync 並不同步(猜測)

wx.getSystemInfoSync 只是在頁面初始化時提前計算。所以對於 windowHeight 這種需要進行功能判斷的屬性,應該使用異步接口,實時獲取

2. wx.getSystemInfo 調用的時機不當

上面講了 windowHeight 的定義,所以這個值取決於 tabbar 是否存在

為了保證 tabbar 顯示后再進行取值,建議在生命周期的 onReady 鈎子中調用接口 wx.getSystemInfo

最終方案

  1. 采用異步接口 wx.getSystemInfo
  2. onReady 中調用

代碼

  onReady() {
    wx.getSystemInfo({
      success({windowHeight}) {
        // todo
      }
    });
  }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM