uniapp自定义头部(兼容微信小程序(胶囊和状态栏),兼容h5)

很早之前就写过自定义头部,但是那时偷懒写死了,现在用插槽重新写了个
有两种形式:
type1是完全自定义的,可以自己去组件改也可以用插槽改
type2是正常的返回标题和右边按钮,使用就是 title="标题" @rightClick = "函数",右边按钮是插槽,可以和下面例子一样用

效果图

类型1
在这里插入图片描述

类型2 的h5
在这里插入图片描述
类型2的小程序
在这里插入图片描述

使用

<myHead title="首页" type="2">
	<view slot="right">
		<text>提交</text>
	</view>
</myHead>

组件代码

<template>
	<view class="myHead">
		<!-- myHead的占位 -->
		<view class="myHead_placeholder" :style="{
				height: addUnit(height + statusBarHeight,'px')
			}">
		</view>
		<view class="myHead_fixed" :style="{'paddingRight': capsuleWidth + 'px'}">
			<!-- 状态栏 -->
			<view class="status_bar" :style="{
				height: addUnit(statusBarHeight)
			}"></view>
			<!-- 头部本体 -->
			<slot>
				<view v-if="type == 1" class="myHead_content" :style="{
					height: addUnit(height)
				}">
					<image src="/static/img/logo.png"></image>
					<text>为牛只-屠宰全产业护航{{capsuleWidth}}</text>
				</view>
				<view v-else class="myHead_content2" :style="{
					height: addUnit(height)
				}">
					<view class="left" @click="leftClick">
						<text class="iconfont icon-left"></text>
					</view>
					<view class="content">
						{{title}}
					</view>
					<view class="right" @click="rightClick">
						<text>提交</text>
					</view>
				</view>
			</slot>
		</view>
	</view>
</template>

<script>
	export default {
		name: "myHead",
		props: {
			// 点击左侧区域(返回图标),是否自动返回上一页
			autoBack: {
				type: Boolean,
				default: false
			},
			title: {
				type: String | Number,
				default: '标题'
			},
			type: {
				type: String | Number,
				default: 2
			}
		},
		data() {
			return {
				// 状态栏
				statusBarHeight: 0,
				// 头部本体高度
				height: 44,
				// 如果有胶囊
				capsuleWidth: 0
			};
		},
		mounted() {
			// 手机状态栏的高度
			this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight || 0
			this.getNavBarHeight()
		},
		methods: {
			// 获取胶囊宽度
			getNavBarHeight() {
				// 小程序环境
				// #ifdef MP-WEIXIN
				const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
				uni.getSystemInfo({
					success: (systemInfo) => {
						this.capsuleWidth = menuButtonInfo.width + systemInfo.screenWidth - menuButtonInfo
							.right; // 胶囊宽度
					}
				})
				// #endif
				// #ifdef H5
				this.capsuleWidth = 0
				// #endif
			},
			addUnit(num, unit = 'px') {
				return num + unit
			},
			// 点击左侧区域
			leftClick() {
				// 如果配置了autoBack,自动返回上一页
				this.$emit('leftClick')
				if (this.autoBack) {
					uni.navigateBack()
				}
			},
			// 点击右侧区域
			rightClick() {
				this.$emit('rightClick')
			},
		}
	}
</script>

<style lang="scss" scoped>
	.myHead {
		.myHead_placeholder {
			width: 100%;
		}

		.myHead_fixed {
			position: fixed;
			left: 0;
			right: 0;
			top: 0;
			z-index: 11;

			.myHead_content {
				display: flex;
				align-items: center;

				image {
					width: 150rpx;
					margin-right: 10rpx;
				}

				text {
					font-size: 24rpx;
					color: rgba(29, 37, 65, 0.5);
				}
			}

			.myHead_content2 {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0 20rpx;

				.left {
					font-size: 28rpx;
				}

				.content {
					flex: 1;
					text-align: center;
					font-size: 32rpx;
				}

				.right {
					font-size: 28rpx;
				}
			}
		}
	}
</style>
<think>好的,我现在需要解决用户在uni-app开发微信小程序时遇到的导航栏胶囊按钮布局或样式问题。首先,我需要明确用户的具体需求。导航栏通常包括标题胶囊按钮,而胶囊按钮的位置样式在不同设备上可能会有差异,特别是在不同屏幕尺寸或系统版本下。用户可能遇到了导航栏高度不适配、胶囊按钮位置偏移或者样式不一致的问题。 接下来,我需要回忆uni-app微信小程序的相关文档。微信小程序胶囊按钮位置是固定的,由微信客户端控制,开发者无法直接修改其位置或样式。但可以通过API获取胶囊按钮的坐标信息,从而调整导航栏的布局。uni-app作为一个跨平台框架,可能需要使用条件编译来处理微信小程序特有的逻辑。 然后,我应该考虑如何获取胶囊按钮的位置信息。根据微信小程序文档,可以使用`wx.getMenuButtonBoundingClientRect()`方法获取胶囊按钮的尺寸位置。这个API返回的top、right、bottom、left等属性可以帮助计算出导航栏的高度布局。 导航栏的高度计算需要注意状态栏的高度加上胶囊按钮的高度。状态栏高度可以通过`wx.getSystemInfoSync().statusBarHeight`获取。胶囊按钮的高度通常是32px,但不同设备可能有差异,所以最好动态获取。 接下来,用户可能需要将这些信息应用到uni-app的页面结构中。比如,在页面的最顶部设置一个占位视图,高度等于导航栏总高度,确保页面内容不会被遮挡。同时,自定义导航栏的样式需要覆盖原生导航栏,这需要在pages.json中设置`"navigationStyle": "custom"`。 在样式方面,可能需要使用绝对定位或固定定位来放置自定义导航栏的元素,确保其与胶囊按钮对齐。同时,需要注意不同设备的适配,使用rpx单位来保证样式的一致性。 此外,用户可能会遇到自定义导航栏中的元素点击区域与胶囊按钮冲突的问题。这时候需要通过计算胶囊按钮的位置,调整自定义元素的布局,避免重叠。 最后,需要提醒用户在测试时使用真机预览,因为开发工具模拟器可能真机表现不一致。同时,考虑到微信客户端版本更新可能带来的变化,建议定期检查API的兼容性。 总结步骤应该是:获取胶囊按钮位置信息,计算导航栏高度,自定义导航栏结构,调整样式适配,处理交互冲突,并进行真机测试。同时提供代码示例配置说明,帮助用户具体实施。</think>### Uniapp微信小程序导航栏与胶囊按钮布局解决方案 在微信小程序开发中,导航栏与胶囊按钮的布局适配需要特别注意以下关键点: **1. 获取胶囊按钮位置** ```javascript const menuButtonInfo = wx.getMenuButtonBoundingClientRect(); const systemInfo = wx.getSystemInfoSync(); // 计算导航栏总高度 const navBarHeight = menuButtonInfo.top + menuButtonInfo.height + 6; // 状态栏高度(单位px) const statusBarHeight = systemInfo.statusBarHeight; ``` **2. 页面结构配置** 在`pages.json`中配置自定义导航栏: ```json { "path": "pages/index/index", "style": { "navigationStyle": "custom", "app-plus": { "titleNView": false } } } ``` **3. 页面布局实现** ```html <template> <!-- 状态栏占位 --> <view :style="{ height: statusBarHeight + 'px' }"></view> <!-- 自定义导航栏 --> <view class="custom-nav" :style="{ height: navBarHeight + 'px' }"> <view class="nav-title">我的小程序</view> </view> </template> ``` **4. 样式适配要点** ```css /* 使用绝对定位避开胶囊按钮区域 */ .custom-nav { position: relative; padding-right: calc(100vw - {{menuButtonInfo.left}}px); } /* 响应式单位转换 */ .nav-title { font-size: 16px; line-height: 32rpx; margin-left: 20rpx; } ``` **5. 多平台适配策略** ```javascript // #ifdef MP-WEIXIN const capsule = wx.getMenuButtonBoundingClientRect(); // #endif // #ifdef H5 // 使用浏览器默认导航栏处理 // #endif ``` **常见问题处理:** 1. **布局错位**:确保使用`rpx`单位,避免固定像素值 2. **点击穿透**:通过`z-index`控制层级关系 3. **iPhone适配**:特别处理`safe-area-inset-top`值 4. **安卓兼容**:注意不同厂商的状态栏高度差异 **最佳实践建议:** - 使用`uni.getSystemInfoSync()`获取设备信息 - 开发阶段使用`console.log`输出胶囊按钮坐标 - 发布前需在iOS/Android真机测试 - 定期检查微信开发者工具更新日志[^2]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值