uniapp 中如何解决H5、小程序、app内容区域不一致问题

追格官方小助手/ 2022年03月25日/ 小程序/ 浏览 3912

uni-app 提供内置 CSS 变量,用来解决H5、小程序、App不同平台内容区域不一致的问题



var(--status-bar-height) 此变量在微信小程序环境为固定 25px,在 App 里为手机实际状态栏高度。


当设置 "navigationStyle":"custom" 取消原生导航栏后,由于窗体为沉浸式,占据了状态栏位置。此时可以使用一个高度为 var(--status-bar-height) 的 view 放在页面顶部,避免页面内容出现在状态栏。


由于在 H5 端,不存在原生导航栏和 tabbar,也是前端 div 模拟。如果设置了一个固定位置的居底 view,在小程序和 App 端是在 tabbar 上方,但在 H5 端会与 tabbar 重叠。此时可使用--window-bottom,不管在哪个端,都是固定在 tabbar 上方。


目前 nvue 在 App 端,还不支持 --status-bar-height变量,替代方案是在页面 onLoad 时通过 uni.getSystemInfoSync().statusBarHeight 获取状态栏高度,然后通过 style 绑定方式给占位 view 设定高度。下方提供了示例代码


使用举例:


.status_bar {
	height: var(--status-bar-height);
	width: 100%;
}

.toTop {
	bottom: calc(var(--window-bottom) + 10px);
}


以上内容来自uniapp官方文档:https://uniapp.dcloud.io/tutorial/syntax-css.html#css-%E5%8F%98%E9%87%8F

发表评论

暂无评论,抢个沙发...

客服 工单