uniapp自定义顶部导航组件

第一步 新建 components/uni-title/uni-title.vue组件

<!-- 自定义标题组件 -->
<template>
	<view class="container">
		<!-- 自定义顶部标题 -->
		<view class="head" :style="{ paddingTop: paddingTopNum + 'px' }">
			<!-- 标题插槽 -->
			<slot name="title"></slot>
		</view>
		<view :style="{ paddingTop: pageTopHeight + 'px' }">
			<!-- 内容插槽插槽 -->
			<slot name="content"></slot>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 导航区高度
			paddingTopNum: uni.getSystemInfoSync().statusBarHeight + 7,
			// 内容区高度
			pageTopHeight: uni.getSystemInfoSync().statusBarHeight + 30 + 7
		};
	}
};
</script>

<style scoped>
/* 自定义导航栏 */
.head {
	position: fixed;
	width: 100%;
	z-index: 999;
	height: 30px;
	line-height: 30px;
	color: #fff;
	font-size: 32rpx;
	/* 设置背景为透明色 */
	background-color: rgba(0,0,0,0);
	top: 0;
	left: 0;
}
</style>

第二步在pages.json中加入easycom配置

"easycom": { //组件化配置
		"autoscan": true, //是否开启自动扫描,开启后将会自动扫描符合components/组件名称/组件名称.vue目录结构的组件
		"custom": {
			// "uni-(.*)": "@/components/uni-$1.vue", // 匹配components目录内的vue文件
			"uni-(.*)": "@/components/uni-$1/uni-$1.vue", // 匹配components目录下组件名称/组件名称内的vue文件
			"vue-file-(.*)": "packageName/path/to/vue-file-$1.vue" // 匹配node_modules内的vue文件
		}
	}

第三步 在uniapp中使用

<template>
	<view class="container">
		<uni-title>
			<!-- 标题区域 -->
			<view slot="title" class="title">
				<uni-icons type="back" size="18" @click="back_page" color="#000"></uni-icons>
				<view>标题</view>
			</view>
			<!-- 内容区域 -->
			<view slot="content" class="income_content">
				
			</view>
		</uni-title>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				
			}
		},
		methods:{
			// 返回点击事件
			back_page(){
				uni.navigateBack({
				    delta: 1
				});
			}
		}
	}
</script>

<style>
page {
	width: 100%;
	height: 100%;
}
.title {
	padding: 0 10rpx;
	display: flex;
	align-items: center;
}
.title uni-icons {
	position: relative;
	left: 0;
}
/* #ifdef APP-PLUS */
.title > view {
	width: 100%;
	text-align: center;
	color: #000;
}
/* #endif */
/* #ifdef MP-WEIXIN */
.title > view {
	width: 100%;
	padding: 0 30rpx;
	color: #000;
}
/* #endif */
/* 内容部分 */
.income_content{
	height: 200rpx;
	border:5rpx solid #000;
	border-radius: 12rpx;
	margin: 0 auto;
}
</style>

项目结果查看

在这里插入图片描述
可以在uniapp和微信小程序中使用,切换不同的手机型号也相应不同增加或减少自定义标题栏的高度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值