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>

二、效果:


相关推荐
mayaairi6 小时前
Vue2 生命周期完全指南:从创建到销毁的8个钩子函数
前端·javascript·vue.js
研☆香7 小时前
一个简单的气泡弹窗制作
javascript
大模型丫丫7 小时前
用 TypeScript、NestJS 和 LangGraph 搭建一个可控的 AI Agent
javascript·人工智能·typescript
IMPYLH7 小时前
HTML 的 <pre> 元素
前端·javascript·html
shmily麻瓜小菜鸡8 小时前
JavaScript / TypeScript 易踩坑知识点 —— 异步编程类
开发语言·javascript·typescript
tsumikistep8 小时前
【前端】Vue + Axios 跨域问题实战:7070 代理转发 8080 + 401 报错分析(黑马外卖)
前端·javascript·vue.js
DK185838322529 小时前
【源码开源部署】电竞代练护航陪玩小程序:全游戏服务平台完整解决方案(附宝塔部署教程)
游戏·微信小程序·uni-app·开源·php
里欧跑得慢9 小时前
Flutter主题与样式详解
前端·css·flutter·web
toooooop89 小时前
智慧文旅数字人体验平台demo html
css·css3
码云数智-大飞9 小时前
2026 年跨端开发决战:小程序原生 vs uni-app vs Taro 深度对比
小程序·uni-app·taro