Uniapp中自定义导航栏

一、代码:

复制代码
<template>
	<view class="page" :style="{ paddingTop: navbarHeight + 'px' }">
		<view class="navbar" :style="{ paddingTop: statusBarHeight + 'px' }">
			<view class="navbar-left" @click="goBack">
				<view class="navbar-left__arrow"></view>
			</view>
			<view class="navbar-title">{{title}}</view>
		</view>
		<view class="content">
			这里是内容区域 -- 渐变的内容
		</view>
		<!-- <view class="content2">
			这里是内容区域 -- 普通的内容
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '这是自定义标题',
				navbarHeight: 0, // 导航栏高度
				statusBarHeight: 0, // 状态栏的高度
			}
		},
		onLoad() {
			const systemInfo = uni.getSystemInfoSync();
			this.statusBarHeight = systemInfo.statusBarHeight;
			this.navbarHeight = this.statusBarHeight + 44; // 44是导航栏标准高度
		},
		onShow() {
			this.init()
		},
		methods: {
			init() {
				// 初始化页面
			},
			goBack() {
				// 获取当前页面栈
				const pages = getCurrentPages();
				if (pages.length > 1) {
					uni.navigateBack(); // 关闭当前页面,返回上一个页面
				} else {
					uni.redirectTo({  // 关闭当前页面,跳转到别的页面
						url: '/pages/index/index'
					});
				}
			},
		}
	}
</script>

<style scoped>
	.page {
		width: 100vw;
		height: 100vh;
		background-color: #F9F9FB;
	}

	.page .navbar {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 999;
		display: flex;
		align-items: center;
	}

	.page .navbar-title {
		flex: 1;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		font-weight: 600;
		font-size: 32rpx;
		color: #000000;
	}

	.page .content {
		position: absolute;
		top: 0;
		left: 0;
		width: 100vw;
		height: 420rpx; /*渐变的高度*/ 
		background: linear-gradient(180deg, #C6EBFD 0%, #F9F9FB 100%);
		padding-top: calc(var(--status-bar-height) + 88rpx);
	}
	.page .content2 {
		padding: 32rpx 30rpx;
		background: pink;
	}
</style>

二、效果:


相关推荐
tangdou3690986552 小时前
AI真好玩系列-2分钟快速了解DeepAgents | Quick Guide to DeepAgents in 2 Minutes
前端·javascript·后端
张元清2 小时前
React useIntersectionObserver Hook:懒加载与可见性检测(2026)
javascript·react.js
彭于晏爱编程2 小时前
纯 JS + Node,一个下午手搓了能读懂公司代码的 AI 助手,老板以为我转行了
前端·javascript
妙码生花3 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十四):眨眼小人登录页制作
前端·javascript·ai编程
妙码生花3 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十三):前端路由初始化
前端·javascript·ai编程
PBitW3 小时前
GPT训练我的第四天,被打惨了!!!😭😭😭
前端·javascript·面试
DarkLONGLOVE3 小时前
快速上手 Pinia!Vue3 极简状态管理使用教程
javascript·vue.js
mackbob3 小时前
.eslintrc.js详细配置说明
javascript
ZhengEnCi4 小时前
Q06-导航按钮高级拟态玻璃效果构建完全指南
前端·css
宸翰4 小时前
解决 uni-app App 端 vue-i18n 占位符丢失:封装跨端可用的 tf 格式化方法
前端·vue.js·uni-app