uniapp 封装自定义头部导航栏

封装原因

项目中有时候需要使用自定义的头部导航栏,原生的无法满足需求

参数

属性名 描述 示例
title 标题 字符串:'首页'
bgColor 背景色 字符串:'#fff'
type 左侧的操作内容 字符串:'all',详细值请在下方查看

参数解释

type

  • all 有返回和回到首页
  • back 只有返回
  • home 只有回到首页
  • none 什么都没有

注意点

  • 组件有一个默认插槽,如果有特殊要求的导航栏可以使用插件进行自定义
  • title和type都是默认插槽中内容的值,所以如果你使用了插槽来自定义头部导航栏的话title和type会失效

代码

javascript 复制代码
<template>
	<view
		class="navbar-box"
		:style="{ 'background-color': props.bgColor }"
		v-if="statusBarHeight && navBarHeight"
	>
		<!-- 状态栏 -->
		<view :style="{ height: statusBarHeight + 'px' }"></view>
		<!-- 导航栏 -->
		<view :style="{ height: navBarHeight + 'px' }">
			<slot>
				<div class="navbar-default">
					<div class="default-left" v-if="props.type != 'none'">
						<view class="default-all" v-if="props.type == 'all'">
							<u-icon name="arrow-left" color="#fff" size="20" @click="goBack"></u-icon>
							<view class="default-line"></view>
							<u-icon name="home" color="#fff" size="20" @click="goHome"></u-icon>
						</view>
						<view class="default-back" v-if="props.type == 'back'">
							<u-icon name="arrow-left" color="#fff" size="20" @click="goBack"></u-icon>
						</view>
						<view class="default-home" v-if="props.type == 'home'">
							<u-icon name="home-fill" color="#fff" size="20" @click="goHome"></u-icon>
						</view>
					</div>
					<view class="default-title">{{ props.title }}</view>
				</div>
			</slot>
		</view>
	</view>
	<!-- 占位 -->
	<view :style="{ height: statusBarHeight + navBarHeight + 'px' }"></view>
</template>
<script setup>
	import { ref, onMounted, computed, watchEffect } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	const props = defineProps({
		title: {
			type: String,
			default: '',
		},
		bgColor: {
			type: String,
			default: '#fff',
		},
		type: {
			type: String,
			default: 'all',
		},
	});

	onMounted(() => {
		geStatusBarHeight();
		getNavBarHeight();
	});

	let statusBarHeight = ref(0);
	let navBarHeight = ref(0);

	// 获取状态栏高度
	function geStatusBarHeight() {
		statusBarHeight.value = uni.getSystemInfoSync()['statusBarHeight'];
	}
	// 获取导航栏高度
	function getNavBarHeight() {
		// #ifdef MP-WEIXIN
		let menuButtonInfo = uni.getMenuButtonBoundingClientRect(); // 胶囊信息
		// 导航栏高度 = 胶囊高度 + 上间距 + 下间距 + 微调
		navBarHeight.value =
			menuButtonInfo.height +
			(menuButtonInfo.top - uni.getSystemInfoSync()['statusBarHeight']) * 2 +
			2;
		// #endif
		// #ifdef APP-PLUS || H5
		navBarHeight.value = 44;
		// #endif
	}

	// 返回上一页
	function goBack() {
		const pages = getCurrentPages();
		if (pages.length == 1) {
			uni.reLaunch({
				url: '/pages/home/index',
			});
			return;
		}
		uni.navigateBack();
	}
	// 去首页
	function goHome() {
		uni.reLaunch({
			url: '/pages/home/index',
		});
	}
</script>

<style lang="scss" scoped>
	.navbar-box {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 2000;
		.navbar-default {
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			position: relative;
			.default-left {
				position: absolute;
				top: 50%;
				left: 24rpx;
				transform: translateY(-50%);
				.default-all {
					display: flex;
					justify-content: center;
					align-items: center;
					padding: 10rpx 30rpx;
					background: rgba(0, 0, 0, 0.5);
					border-radius: 32rpx;
					.default-line {
						width: 2rpx;
						height: 40rpx;
						background: #afafaf;
						margin: 0 20rpx;
					}
				}
				.default-back,
				.default-home {
					width: 63rpx;
					height: 63rpx;
					background: rgba(0, 0, 0, 0.5);
					border-radius: 50%;
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}
			.default-title {
				color: #000;
				font-weight: bold;
				font-size: 16px;
			}
		}
	}
</style>
相关推荐
瑶琴AI前端4 小时前
HBuilderX uniapp项目转vue-cli项目完整步骤(已成功)
前端·vue.js·uni-app
苏州第一深情5 小时前
【uniapp】uniapp实现单点登录、打包H5部署到线上
javascript·vue.js·uni-app
anyup7 小时前
uView Pro 正式开源!70+ Vue3 组件重构全记录,助力 uni-app 组件生态,你会选择吗?
前端·架构·uni-app
阿奇__9 小时前
uniapp 类似popover气泡下拉框组件
前端·css·uni-app
泡壶好茶9 小时前
uniapp Android App集成支付宝的扫码组件mPaaS
uni-app·扫码组件mpaas
绍棠9 小时前
uniapp转app时,cover-view的坑
前端·javascript·uni-app
QZ_orz_freedom9 小时前
uni-app学习笔记01-项目初始化及相关文件
笔记·学习·uni-app
游戏开发爱好者810 小时前
Fiddler中文版使用指南 提升开发流程的一站式抓包与调试体验
android·ios·小程序·https·uni-app·iphone·webview
顽疲11 小时前
从零用java实现小红书springboot_vue_uniapp(15)评论和im添加图片
java·vue.js·spring boot·uni-app
2501_9159214321 小时前
移动端 WebView 视频无法播放怎么办 媒体控件错误排查与修复指南
android·ios·小程序·https·uni-app·iphone·webview