uniapp顶部导航栏高度适配

为了实现好看又实用的顶部导航栏,不得不自己定义导航栏样式。而自己定义的导航栏高度会因为手机的型号不同所展示的效果也就不同,所以只能通过适配高度来达到预期的效果

1.需要在page.json文件中对需要自定义导航栏文件进行配置

"navigationStyle":"custom" //取消默认的原生导航栏

2.在页面中添加占位元素

html 复制代码
		<!-- 状态栏高度 -->
<view :style="{ height: `${statusBarHeight}px` }"></view>

 <!-- 自定义导航栏高度  -->
        <view :style="{
          height: `${barHeight}px`,
          'line-height': `${barHeight}px`
        }">
            <text>评价</text>
        </view>

3.借助钩子函数mounted完成逻辑处理

javascript 复制代码
	mounted() {
			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
			const {
				top,
				height
			} = wx.getMenuButtonBoundingClientRect();
			// 自定义导航栏高度 = 胶囊高度 + 胶囊的padding*2, 如果获取不到设置为38
			this.barHeight = height ? height + (top - this.statusBarHeight) * 2 : 38;
		},

这样就行了,不同的设备导航栏高度就会自适应了

相关推荐
码事漫谈1 小时前
当AI开始“思考”:我们是否真的准备好了?
前端·后端
许杰小刀2 小时前
ctfshow-web文件包含(web78-web86)
android·前端·android studio
我是Superman丶2 小时前
Element UI 表格某行突出悬浮效果
前端·javascript·vue.js
恋猫de小郭2 小时前
你的代理归我了:AI 大模型恶意中间人攻击,钱包都被转走了
前端·人工智能·ai编程
xiaokuangren_3 小时前
前端css颜色
前端·css
Huanzhi_Lin3 小时前
关于V8/MajorGC/MinorGC——性能优化
javascript·性能优化·ts·js·v8·新生代·老生代
hoiii1873 小时前
C# 基于 LumiSoft 实现 SIP 客户端方案
前端·c#
anOnion3 小时前
构建无障碍组件之Meter Pattern
前端·html·交互设计
小码哥_常4 小时前
Spring Boot配置diff:解锁配置管理新姿势
前端
小码哥_常4 小时前
告别onActivityResult!Android数据回传的3大痛点与终极解决方案
前端