windowHeight
概念
可使用窗口高度,即:屏幕高度(screenHeight
) - 導航(tabbar
)高度
存在問題
安卓設備下獲取 windowHeight
不能准確得到對應的高度,總是拿到屏幕高度
原因
1. 同步接口 wx.getSystemInfoSync
並不同步(猜測)
wx.getSystemInfoSync
只是在頁面初始化時提前計算。所以對於 windowHeight
這種需要進行功能判斷的屬性,應該使用異步接口,實時獲取
2. wx.getSystemInfo 調用的時機不當
上面講了 windowHeight
的定義,所以這個值取決於 tabbar
是否存在
為了保證 tabbar
顯示后再進行取值,建議在生命周期的 onReady
鈎子中調用接口 wx.getSystemInfo
最終方案
- 采用異步接口
wx.getSystemInfo
- 在
onReady
中調用
代碼
onReady() {
wx.getSystemInfo({
success({windowHeight}) {
// todo
}
});
}