uniapp自定义navigationBar

最终效果:

一、关闭默认导航栏

pages.json文件中,对单个页面关闭

"navigationStyle": "custom"

javascript 复制代码
"pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
		{
			"path": "pages/index/index",
			"style": {
				"navigationBarTitleText": "控制台",
				"navigationStyle": "custom"
			}
		},
		{
			"path" : "pages/message/message",
			"style" : 
			{
				"navigationBarTitleText" : "消息"
			}
		}
		
	],

二、组件设计

html 复制代码
<template>
	<view>
		<!-- 状态栏占位 -->
		<view :style="{paddingTop: statusBarHeight + 'px'}"></view>
		<!-- 导航栏内容 -->
		<view :style="{height: navBarHeight + 'px'}" class="navBarComponent">
			<view class="navContent">
				<text class="title">{{titles}}</text>
				<view class="right" :style="{width: count*40 + 'px'}" v-if="count > 0" >
					<view class="item" v-for="(item, index) in icons" :key="index">
						<image :src="item.icon" class="imageShow" @click="toPage(item.link)"></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name:"navigationBar",
		data() {
			return {
				statusBarHeight: 0,
				navBarHeight: 0,
				count: 0,
				icons: []
			};
		},
		props:{
			titles: String,
			links: Array
		},
		methods: {
			toPage(url){
				uni.navigateTo({
					url:url
				})
			}
		},
		created() {
			// 动态获取手机状态栏高度(电量显示区域),H5没有状态栏,会为0
			this.statusBarHeight = uni.getSystemInfoSync()['statusBarHeight'];
			// #ifdef H5
			this.navBarHeight = 45
			// #endif
			// #ifndef H5
			this.navBarHeight = this.statusBarHeight + 11
			// #endif
			
			if(typeof this.$props.links === 'undefined'){
				this.count = 0
			}else{
				this.count = this.$props.links.length
				this.icons = this.$props.links
			}
		}
	}
</script>

<style lang="scss">
	.navBarComponent {
		display: flex;
		align-items: center;
		box-sizing: border-box;
		
		.navContent{
			display: flex;
			align-items: center;
			padding-left: 15px;
			padding-right: 15px;
			width: 100%;
			.title{
				font-size: 24px;
				flex: 1;
			}
			.right{
				display: flex;
				width: 120px;
				height: 100%;
				align-items: center;
				justify-content: space-between;
				.item{
					background-color: white;
					width: 36px;
					height: 36px;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
					.imageShow{
						width: 60%;
						height: 60%;
					}
				}
			}
		}
	}
</style>

三、使用

html 复制代码
<navigationBar :titles="'控制台'" :links="icons"></navigationBar>

图标链接数据:

javascript 复制代码
this.icons = [
	{
	    icon: '/static/icon/notify.png',
	    link: '/pages/message/message'
	},
	{
		icon: '/static/icon/scan.png',
		link: '/pages/message/message'
	}
]

不同框架下的语法会有区别

相关推荐
Hilaku2 分钟前
为什么跳槽涨薪 30% 的时代彻底结束了?聊聊 2026 前端薪资的天花板
前端·javascript·程序员
愚公搬代码14 分钟前
【愚公系列】《Web应用安全》006-HackBar插件的使用
前端·安全
超爱吃香菜的菜鸟35 分钟前
关于pnpm 部分使用(一)
前端·vue.js
Vuji42 分钟前
Pi 插件解剖|git-checkpoint.ts:只用 53 行,让 fork 恢复代码状态
前端·agent
大家的林语冰1 小时前
✌️ 让 Rust 再次伟大,pnpm 12 抛弃 TypeScript,移植 Rust 原地起飞!
前端·javascript·node.js
XPoet2 小时前
AI 编程工程化:实战——从 0 到 1 搭建 AI 编程工作流
前端·后端·ai编程
-今昭-2 小时前
Logstash 管理
java·服务器·前端
名字还没想好☜2 小时前
React 用自定义 useDebounce Hook 治好搜索框频繁请求:防抖、竞态取消与卸载清理
前端·javascript·react.js·react·hooks
杉氧3 小时前
Flutter 跨平台多端适配与 Android/iOS 一键自动化打包发布
android·前端·flutter
障碍的枫子3 小时前
xss和CSRF的攻击和防御
前端·xss·csrf