/** * 顶部导航栏与胶囊避让度量工具 * 规范化统一各页面的 Topbar 状态栏高度、导航栏高度及微信小程序右上角胶囊避让 */ /** * 获取当前设备与平台环境下的导航栏尺寸度量 * @returns {Object} 包含 statusBarHeight, navBarHeight, capsulePadding, capsuleWidth, capsuleHeight, capsuleTop, totalHeight */ export function getNavMetrics() { let statusBarHeight = 0 let navBarHeight = 44 let capsulePadding = 0 let capsuleWidth = 0 let capsuleHeight = 32 let capsuleTop = 6 let capsuleRight = 0 try { // #ifdef H5 // 浏览器环境下通常无需模拟系统状态栏,状态栏高度置 0,导航栏高度固定 44px statusBarHeight = 0 navBarHeight = 44 capsulePadding = 0 // #endif // #ifndef H5 const sys = uni.getSystemInfoSync ? uni.getSystemInfoSync() : null if (sys && sys.statusBarHeight) { statusBarHeight = sys.statusBarHeight } // #ifdef MP-WEIXIN || MP if (typeof uni.getMenuButtonBoundingClientRect === 'function') { const menu = uni.getMenuButtonBoundingClientRect() if (menu && menu.width && menu.top) { capsuleWidth = menu.width capsuleHeight = menu.height capsuleTop = menu.top const winWidth = (sys && sys.windowWidth) ? sys.windowWidth : 375 capsuleRight = winWidth - menu.right // 右侧避让距离 = 胶囊宽度 + 胶囊右边距 + 左右视觉留白间隙(8px) capsulePadding = (winWidth - menu.left) + 8 // 胶囊垂直居中时的标准导航条高度:上下边距对称 (menu.top - statusBarHeight) * 2 + menu.height const barH = (menu.top - statusBarHeight) * 2 + menu.height if (barH >= 38 && barH <= 64) { navBarHeight = barH } } else { capsulePadding = 96 } } // #endif // #endif } catch (e) { capsulePadding = 0 } return { statusBarHeight, navBarHeight, capsulePadding, capsuleWidth, capsuleHeight, capsuleTop, totalHeight: statusBarHeight + navBarHeight, } } export default { getNavMetrics, }