nuiapp 自定义navbar

代码示例

navbar 组件

// navbar 组件

<template>
	<view class="layout">
        <!-- 状态栏 -->
		<view class="status-bar"></view>

		<view class="navbar">
			<text class="navbar-title">{{props.title}}</text>
			<navigator url="/pages/search/index" class="search-section">
				<uni-icons class="icon" type="search" color="#888" size="18"></uni-icons>
				<text class="text">搜索</text>
			</navigator>
		</view>
	</view>
	<!-- 填充块 -->
	<view class="fill"></view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		getStatusBarHeight,
		getNavBarHeight,
		getFillHeight
	} from "@/utils/system.js"

	const props = defineProps({
		title: {
			type: String,
			default: '推荐'
		}
	})

	// 状态栏的高度
	const statusBarHeight = ref(getStatusBarHeight() * 2 + 'rpx')
	// 导航栏高度
	const navbarHeight = ref(getNavBarHeight() * 2 + 'rpx')
	// 填充高度
	const fillHeight = ref(getFillHeight() * 2 + 'rpx')
</script>

<style scoped lang="scss">
	.layout {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 10;
		background:
			linear-gradient(to bottom, transparent, #fff 400rpx),
			linear-gradient(to right, #beecd8 20%, #F4E2D8);

		.status-bar {
			width: 100%;
			height: v-bind(statusBarHeight);
		}

		.navbar {
			display: flex;
			align-items: center;
			height: v-bind(navbarHeight);
			margin-left: 30rpx;

			&-title {
				font-weight: 600;
				font-size: 36rpx;
				color: #000;
			}

			.search-section {
				width: 220rpx;
				height: 50rpx;
				border-radius: 60rpx;
				background: rgba(255, 255, 255, 0.4);
				border: 1px solid #fff;
				margin-left: 30rpx;
				color: #999;
				font-size: 28rpx;
				display: flex;
				align-items: center;

				.icon {
					margin-left: 5rpx;
				}

				.text {
					padding-left: 10rpx;
				}
			}
		}
	}

	.fill {
		width: 100%;
		height: v-bind(fillHeight);
	}
</style>

system.js 

// system.js

const SYSTEM_INFO = uni.getSystemInfoSync();

// 手机状态栏高度
export const getStatusBarHeight = () => SYSTEM_INFO.statusBarHeight || 20;

// 导航栏高度
export const getNavBarHeight = () => {
	if (uni.getMenuButtonBoundingClientRect) {
		let {
			top,
			height
		} = uni.getMenuButtonBoundingClientRect();
		return height + (top - SYSTEM_INFO.statusBarHeight) * 2 // 按rpx计算 故 *2
	} else {
		return 48;
	}
}

// 填充块高度
export const getFillHeight = () => getStatusBarHeight() + getNavBarHeight();

效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值