uniapp实现自定义导航内容高度居中(兼容APP端以及小程序端与胶囊对齐)

①效果图如下

1.小程序端与胶囊对齐

2.APP端内容区域居中

注意:上面使用的是colorui里面的自定义导航样式。

②思路:

1.APP端和小程序端走不同的方法,因为小程序端要计算不同屏幕下右侧胶囊的高度。

2.其次最重要的要清晰App端和小程序端的计算逻辑。

3.然后调用api获取屏幕信息,小程序还需要单独调用获取胶囊的api。

系统信息uni.getSystemInfoSync()

小程序端胶囊信息uni.getSystemInfoSync

4.最后写公共的封装方法,在多个页面调用。

小程序端计算方法:

2.1.头部整体高度 ==状态栏高度 + 导航栏高度

2.2.导航栏高度 == (胶囊距顶部高度-状态栏高度) *2 +胶囊高度

2.3.计算导航内容距离顶部高度= 状态栏高度/2

APP端计算方法:

2.4.计算自定义导航栏的高度=((屏幕高度-状态栏高度)/需要除的比例)

③实现代码

3.1、封装的公共的方法APP端和小程序端

javascript 复制代码
	/*
	 * 共用的自定义导航高度位置(App端)
	 * 在页面中获取系统信息,并计算自定义导航栏的高度
	 * comNum 计算除数
	 * saveFloat 保留小数位数
	 */
	utilsNavbarHeight(screenH, statusH, comNum, saveFloat) {
		const screenHeight = screenH; // 屏幕高度
		const statusBarHeight = statusH; // 状态栏高度
		var saveFloats = 2
		if (saveFloat != undefined) {
			saveFloats = saveFloat
		}
		// 计算自定义导航栏的高度
		const navBarHeight = ((screenHeight - statusBarHeight) / comNum).toFixed(saveFloats); // 例如除以10,可以根据实际需求进行调整
		return navBarHeight
	},

	/*
	 *小程序端与胶囊平行
	 */
	WechatNavBarHeight() {
		//获取状态栏高度
		const statusBarHeight = uni.getSystemInfoSync().statusBarHeight
		//获取小程序胶囊信息
		const menu = uni.getMenuButtonBoundingClientRect()
		//导航栏高度 == (胶囊距顶部高度-状态栏高度) *2 +胶囊高度
		const navBarHeightWechat = (menu.top - statusBarHeight) * 2 + menu.height
		//头部整体高度 ==状态栏高度 + 导航栏高度
		const headerHeight = statusBarHeight + navBarHeightWechat
		//计算导航内容距离顶部高度= 状态栏高度/2
		const topHeight = statusBarHeight / 2 + 'px'
		return {
			topHeight,
			headerHeight
		}
	},

3.2、使用自定义导航栏页面调用

注意:height动态绑定的是navBarHeight,整体导航栏高度

top动态邦定的是statusBarHeight,计算后的距顶部高度

javascript 复制代码
//布局
<view class="Content">
		<!-- 自定义导航 -->
		<view class="navbar">
			<view class="cu-bar bg-blue search" :style="{'height':navBarHeight}">
				<view class="rowList" :style="{'top':statusBarHeight}">
					<view class="action" @click="loca">
						<text>测试</text>
						<text class="cuIcon-triangledownfill"></text>
					</view>
					<view :class="[isWeixin?'search-form radius wechatNavbar':'search-form radius']">
						<text class="cuIcon-search"></text>
						<input @tap.stop="InputFocus" :disabled="true" :adjust-position="false" type="text"
							:placeholder="currentWord" confirm-type="search"></input>
					</view>
					<view class="cu-avatar round" @click="addFunction"
						:style="isWeixin ? 'background-image:url(static/images/index/add.png)' : 'background-image:url(/static/images/index/add.png)'">
					</view>
				</view>
			</view>
		</view>
//初始化数据
	navBarHeight: null,//导航栏高度
				statusBarHeight: null,//导航内容距离整体导航栏高度
				headerHeight: null, //顶部导航整体高度

//方法
//计算导航栏高度
			comNavbarHeight() {
				// #ifdef APP-PLUS
				const devres = this.$system.devInfo()
				const navBarHeight = this.$system.utilsNavbarHeight(devres.screenHeight, devres.statusBarHeight, 8.6, 2)
				this.navBarHeight = navBarHeight + 'px'
				this.statusBarHeight = devres.statusBarHeight / 2 + 'px' //14% 准确来说14%
				this.headerHeight = navBarHeight
				// #endif
				// #ifdef MP-WEIXIN
				const wechatObj = this.$system.WechatNavBarHeight()
				this.statusBarHeight = wechatObj.topHeight
				this.navBarHeight = wechatObj.headerHeight + 'px'
				this.headerHeight = wechatObj.headerHeight
				// #endif
			},

这样就可以了,实现过程中也踩了很多坑,有什么问题评论区留言啊!

相关推荐
qq_4244091911 分钟前
uniapp的app项目,某个页面长时间无操作,返回首页
前端·vue.js·uni-app
2501_9159184113 分钟前
Fiddler中文版全面评测:功能亮点、使用场景与中文网资源整合指南
android·ios·小程序·https·uni-app·iphone·webview
说私域1 小时前
从品牌附庸到自我表达:定制开发开源AI智能名片S2B2C商城小程序赋能下的营销变革
人工智能·小程序
難釋懷1 小时前
第一个小程序
小程序
春哥的研究所1 小时前
可视化DIY小程序工具!开源拖拽式源码系统,自由搭建,完整的源代码包分享
小程序·开源·开源拖拽式源码系统·开源拖拽式源码·开源拖拽式系统
weixin_lynhgworld1 小时前
盲盒一番赏小程序技术实现方案:高并发与防作弊的平衡之道
小程序
不知名It水手2 小时前
uniapp运行项目到ios基座
ios·uni-app·cocoa
hunzi_12 小时前
搭建商城系统
java·uni-app·php
今日热点3 小时前
小程序主体变更全攻略:流程、资料与异常处理方案
经验分享·微信·小程序·企业微信·微信公众平台·微信开放平台
合作小小程序员小小店5 小时前
web网页,在线%食谱推荐系统%分析系统demo,基于vscode,uniapp,vue,java,jdk,springboot,mysql数据库
vue.js·spring boot·vscode·spring·uni-app