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>

二、效果:


相关推荐
溪语流沙几秒前
【每天一个CSS | Day08】整排按钮被鼠标吸走的磁吸导航
前端·css·计算机外设
就叫年华吧丶10 分钟前
长文档点目录定位总是不准?一个 content-visibility + 平滑滚动引发的连环坑(Vue3 实战)
前端·javascript·算法·vue
赵得C18 分钟前
创建 SVG 图标预览页面:从零实现到解决 CORS 问题
前端·javascript·html
peter676840 分钟前
css学习小结
前端·css·学习
liangshanbo121541 分钟前
什么是CSS原子化?它的优劣势是什么?
前端·css·tensorflow·css原子化
ljt27249606611 小时前
Vue笔记(九)--defineEmits
javascript·vue.js·笔记
Flynt11 小时前
Linear 用 1000 个 PR 换掉 styled-components,我写了 200 个按钮,把这笔账复现了一遍
前端·css·preact
Mr_Mao15 小时前
告别选型困难!集 VueUse、ahooks、Mantine 于一身:286 个 Hook 的 ReaUse 来了
前端·javascript·react.js
默_笙16 小时前
🚙 一条腿走路会摔:ES 关键词检索、混合召回与 ReRank 裁判
前端·javascript
00后程序员张16 小时前
苹果App Store上架指南:费用、原理与步骤详解
android·ios·小程序·https·uni-app·iphone·webview