ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

uniapp_微信小程序自定义顶部导航栏和右侧胶囊对齐(不对齐来打我)

2026/9/19 15:47:16 拓冰建站 浏览量
uniapp_微信小程序自定义顶部导航栏和右侧胶囊对齐(不对齐来打我)

一、想要的效果

思路首先开启自定义导航栏,取消自带的导航栏,然后计算胶囊的高度和标题对齐

二、成品代码 

1、首先再你需要居中的代码添加以下style

	<view class="header":style="{paddingTop:navBarTop + 'px',height:navBarHeight + 'px',lineHeight:navBarHeight + 'px'}"></view>

2、模板上写上这几个变量

data(){return{navBarTop: null,navBarHeight: null,
}
}

3、onload计算高度并赋值

onLoad() {let statuBar = uni.getSystemInfoSync().statusBarHeight;let menuButtonInfo = uni.getMenuButtonBoundingClientRect();console.log(menuButtonInfo)this.navBarTop = menuButtonInfo.top;this.navBarHeight = menuButtonInfo.height;},

三、效果