Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { ensureInMiniProgram } from './ensureInMiniProgram'
export interface GetTopBarInfoResult {
/**
* 状态栏高度。
*/
statusBarHeight: number
/**
* 菜单按钮宽度。
*/
menuButtonWidth: number
/**
* 菜单按钮高度。
*/
menuButtonHeight: number
/**
* 菜单按钮垂直外边距。
*/
menuButtonVerticalMargin: number
/**
* 菜单按钮水平外边距。
*
* **注意: QQ 小程序下该项为 0,请自行选取默认值**
*/
menuButtonHorizontalMargin: number
/**
* 导航栏高度。
*/
navigationBarHeight: number
/**
* 顶栏高度。
*/
topBarHeight: number
}
/**
* 获取顶栏信息。
*
* @returns 返回获取到的顶栏信息
*/
export function getTopBarInfo(): GetTopBarInfoResult {
return ensureInMiniProgram(mp => {
const menuRect = mp.getMenuButtonBoundingClientRect()
const sysInfo = mp.getSystemInfoSync()
// 部分情况下 statusBarHeight 可能不存在或为 0,需手动计算,如:
// 苹果手机 iOS 版本 < 13 时下开启热点等
Iif (!sysInfo.statusBarHeight) {
sysInfo.statusBarHeight = sysInfo.screenHeight - sysInfo.windowHeight
}
const statusBarHeight = sysInfo.statusBarHeight
const menuButtonWidth = menuRect.width
const menuButtonHeight = menuRect.height
const menuButtonVerticalMargin = menuRect.top - statusBarHeight
const menuButtonHorizontalMargin = sysInfo.windowWidth - menuRect.right
const navigationBarHeight = menuButtonHeight + menuButtonVerticalMargin * 2
const topBarHeight = statusBarHeight + navigationBarHeight
return {
statusBarHeight,
menuButtonWidth,
menuButtonHeight,
menuButtonVerticalMargin,
menuButtonHorizontalMargin,
navigationBarHeight,
topBarHeight,
}
})
}
|