自定义导航(直接用超简单)

第一步:

先在pages.json文件夹中设置:“navigationStyle”: “custom” (取消默认的原生导航栏)

下面是可以根据业务需求设置单页面自定义导航栏

可以根据业务需求设置单页面自定义导航栏在这里插入图片描述

下面需求设置全体页面面自定义导航栏

在这里插入图片描述

第二步:将顶部状态栏自定义设置封装成组件提高组件复用性

这是一个组件,将顶部状态栏自定义设置封装成组件提高组件复用性(直接用就可以)
<template>
	<view class="_navbar" :style="{ height:`${trStyle.height + trStyle.top + bottomEx}px`}">
		<view class="_navbar_container" :style="{ height:`${trStyle.height}px`,top:`${trStyle.top}px` }">
			 <!-- @click="uni.navigateBack() 返回事件" -->
			<view  class="_navbar_back">
				<i class="iconfont icon-arrow-left-bold" style="font-size:40rpx"></i>
				<!-- 左边 -->
				<view class="zuo">
					<image src="../../static/ylogo.png" mode="" class="img"></image>
				</view>
				<!-- <view class="" style="width: 50rpx;height: 50rpx;">
					<image src="" style="width: 100%;height: 100%;" mode=""></image>
				</view> -->
			</view>
			<!-- 标题 -->
			<view :style="{ height:`${trStyle.height }px`,lineHeight:`${trStyle.height }px` }" class="_navbar_title"> {{ title }}</view>
		</view>
	</view>
</template>

<script>
	export default{
				name:"Navbar",
			data(){
				return {
					trStyle:uni.getMenuButtonBoundingClientRect(),
					background :'transparent',
					bottomEx:10,
				}
			},
			mounted(){	
				uni.$on('pageScroll',top => {
					let parentRoute = this.$parent.$scope.route
					let pages = getCurrentPages()
					let page = pages[pages.length - 1]
					let currentRoute = page.route
					if(parentRoute === currentRoute){
						this.$nextTick(()=>{
							if(top === 0) this.background = 'transparent'
							else if(top > this.trStyle.height + this.trStyle.top + this.bottomEx) this.background = '#2D248B'
							else this.background = `rgba(#2D248B, ${ top/this.trStyle.height + this.trStyle.top + this.bottomEx })`
						})
					}
				})
			},
			beforeUnmount(){
				uni.$off('pageScroll')
			},
			props:{
				back:{
					type:Boolean,
					default:true,
				},
				title:{
					type:String,
					default:'这是个名字'
				}
			},
		}
</script>

<style lang="scss">
	._navbar{
		width:750rpx;
		position:fixed;
		z-index:10000;
		transition:all 0.2s linear;
		background: #fff;
		border-bottom: 1rpx solid rgba(226,226,226,1);
		margin-bottom: 35rpx;
		._navbar_container{
			width:750rpx;
			position:absolute;
			padding: 0 30rpx;
			box-sizing: border-box;
			left:0;
			bottom:0;
			._navbar_back{
				width: 100rpx;
				.img{
					width: 52rpx;
					height: 52rpx;
				}
				color:#000;
				height:100%;
				font-size:40rpx;
				//margin-left:20rpx;
				cursor:pointer;
			}
			._navbar_title{
				position:absolute;
				left:50%;
				top:0rpx;
				height:100%;
				transform:translateX(-50%);
				font-size:30rpx;
				font-family: 'my-font';
				font-weight: 600;
				color:#000;
			}
		}
		
	}
</style>

第三部 将组件引入页面中

把组件引入保持在最上方 必须放第一行 (两个图都是一个页面,哪里用就在哪里写)


在这里插入图片描述

注:只是为了后续自己方便用,方便自己也方便他人
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Uniapp中,可以使用自定义导航栏插件来实现自定义导航栏的功能。以下是实现自定义导航栏的步骤: 1. 安装自定义导航栏插件 在Uniapp项目中使用npm安装自定义导航栏插件,命令如下: ``` npm install uni-navigation-bar ``` 2. 在页面中引入导航栏组件 在需要使用自定义导航栏的页面中,引入导航栏组件,如下所示: ```html <template> <view> <nav-bar :title="title" :background-color="backgroundColor" :color="color" /> <!-- 页面内容 --> </view> </template> <script> import NavBar from 'uni-navigation-bar' export default { components: { NavBar }, data() { return { title: '自定义导航栏', backgroundColor: '#ffffff', color: '#000000' } } } </script> ``` 3. 配置导航栏样式 通过在data中设置title、backgroundColor和color来配置导航栏的样式。其中,title表示导航栏的标题,backgroundColor表示导航栏的背景颜色,color表示导航栏的文字颜色。 4. 实现导航栏返回功能 使用uni-app内置的导航栏返回功能,可以在页面中实现导航栏返回功能。代码如下: ```html <template> <view> <nav-bar :title="title" :background-color="backgroundColor" :color="color" :delta="1" /> <!-- 页面内容 --> </view> </template> <script> import NavBar from 'uni-navigation-bar' export default { components: { NavBar }, data() { return { title: '自定义导航栏', backgroundColor: '#ffffff', color: '#000000' } }, methods: { // 返回上一页 goBack() { uni.navigateBack({ delta: 1 }) } } } </script> ``` 在导航栏中添加返回按钮,并在点击返回按钮时调用goBack方法实现导航栏返回功能。 以上就是在Uniapp中实现自定义导航栏的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值