如何在uniapp中獲取可視區(qū)域的高度(uni.getSystemInfo)
前言
在實際開發(fā)中我們會遇到不確定高度的情況,那么在uniapp中我們如何獲取區(qū)域的高度吶?一起來看看吧
使用到的:
uni-app提供了異步(uni.getSystemInfo)和同步(uni.getSystemInfoSync)的2個API獲取系統信息
uni.getSystemInfo(OBJECT)
異步獲取系統信息
OBJECT 參數說明:
參數名 | 類型 | 必填 | 說明 |
success | Function | 是 | 接口調用成功的回調 |
fail | Function | 否 | 接口調用失敗的回調函數 |
complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執(zhí)行) |
success 返回參數說明,目前需要使用到的:
參數 | 說明 |
windowHeight | 可使用窗口高度 |
statusBarHeight | 手機狀態(tài)欄的高度 |
一、分析uniapp的可視區(qū)域
是哪塊哪?其實和我們想的有些出入
其實就是藍色區(qū)域=紅色區(qū)域-綠色區(qū)域

代碼:
getClineHeight(){
const res = uni.getSystemInfo({
success:(res=>{
this.clientHeight = res.windowHeight-uni.upx2px(80)
})
});
},
tip:注意,在我們的微信小程序中是可以正確顯示的,但是在ios中是有問題的
二、如何在小程序和ios中都能正確顯示
我們只需要獲取系統信息中的platform信息,判斷是ios或者android或者其他
tip注意點:
1.注意這里的單位是pxname我們需要將代碼中導航欄寫的css的80rpx轉換為40px,使用upx2px直接可以進行轉換
2.ios本身有44的高度,Android是48
代碼:
getBarHeight(){
const res = uni.getSystemInfoSync()
if(res.platform==='ios'){
return 44+res.statusBarHeight
}else if(res.platform==='android'){
return 48+res.statusBarHeight
}else{
return 0;
}
},
//獲取可視區(qū)域高度
getClineHeight(){
const res = uni.getSystemInfo({
success:(res=>{
this.clientHeight = res.windowHeight-uni.upx2px(80)-this.getBarHeight();
})
});
},三、減掉的部分
白色部分=綠色部分(windowHeight)-藍色部分(ios44,Android48)-橙色部分(getSystemInfoSync中的statusBarHeight)-黑色部分(你設置的高度或者使用組件的高度,注意單位是px)
windowHeight | 可使用窗口高度 |
windowHeight | 可使用窗口高度 |
減去

代碼:
getBarHeight(){
const res = uni.getSystemInfoSync()
if(res.platform==='ios'){
return 44+res.statusBarHeight
}else if(res.platform==='android'){
return 48+res.statusBarHeight
}else{
return 0;
}
},
//獲取可視區(qū)域高度
getClineHeight(){
const res = uni.getSystemInfo({
success:(res=>{
this.clientHeight = res.windowHeight-uni.upx2px(80)-this.getBarHeight();
})
});
},總結
到此這篇關于如何在uniapp中獲取可視區(qū)域的高度(uni.getSystemInfo)的文章就介紹到這了,更多相關uniapp獲取可視區(qū)域的高度內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

