uni-app開發(4)---自定義導航欄開發

第1步:配置頁面

"app-plus": {
	"scrollIndicator": "none", //隱藏滾動條
	"bounce": "none", //關閉反彈效果
	"titleNView": false
}

注意:"titleNView": false配置可以禁用導航欄。


第2步:引入官方組件uni-icon、uni-nav-bar、uni-status-bar

備註:uni-icon.vue表示圖標組件。uni-status-bar.vue組件表示狀態欄組件。uni-nav-bar.vue表示導航欄組件。


第3步:組件分析

<template>
	<view class="uni-navbar">
		<view :class="{'uni-navbar--fixed': fixed,'uni-navbar--shadow':border,'uni-navbar--border':border}" :style="{'background-color':backgroundColor}" class="uni-navbar__content">
			<!-- 狀態欄 -->
			<uni-status-bar v-if="statusBar" />
			<!-- 中間部分 -->
			<view :style="{color:color}" class="uni-navbar__header">
				<!-- 1、左邊 -->
				<view class="uni-navbar__header-btns" @tap="onClickLeft">
					<view v-if="leftIcon.length">
						<uni-icon :type="leftIcon" :color="color" size="24" />
					</view>
					<view v-if="leftText.length" :class="{'uni-navbar-btn-icon-left':!leftIcon.length}" class="uni-navbar-btn-text">{{ leftText }}</view>
					<slot name="left" />
				</view>
				<!-- 2、中間 -->
				<view class="uni-navbar__header-container">
					<view v-if="title.length" class="uni-navbar__header-container-inner">{{ title }}</view>
					<!-- 標題插槽 -->
					<slot />
				</view>
				<!-- 3、右邊 -->
				<view class="uni-navbar__header-btns" @tap="onClickRight">

					<!-- 優先顯示圖標 -->
					<view v-if="rightText.length&&!rightIcon.length" class="uni-navbar-btn-text">{{ rightText }}</view>
					<slot name="right" />
				</view>
			</view>
		</view>
		<view v-if="fixed" class="uni-navbar__placeholder">
			<uni-status-bar v-if="statusBar" />
			<view class="uni-navbar__placeholder-view" />
		</view>
	</view>
</template>

<script>
	import uniStatusBar from '../uni-status-bar/uni-status-bar.vue'
	import uniIcon from '../uni-icon/uni-icon.vue'

	export default {
		name: 'UniNavBar',
		components: {
			uniStatusBar,
			uniIcon
		},
		props: {
			title: {
				type: String,
				default: ''
			},
			leftText: {
				type: String,
				default: ''
			},
			rightText: {
				type: String,
				default: ''
			},
			leftIcon: {
				type: String,
				default: ''
			},
			rightIcon: {
				type: String,
				default: ''
			},
			fixed: {
				type: Boolean,
				default: false
			},
			color: {
				type: String,
				default: '#000000'
			},
			backgroundColor: {
				type: String,
				default: '#FFFFFF'
			},
			statusBar: {
				type: Boolean,
				default: false
			},
			shadow: {
				type: Boolean,
				default: true
			},
			border: {
				type: Boolean,
				default: true
			}
		},
		methods: {
			onClickLeft() {
				this.$emit('click-left')
			},
			onClickRight() {
				this.$emit('click-right')
			}
		}
	}
</script>

<style>
	@charset "UTF-8";

	.uni-navbar__content {
		display: block;
		position: relative;
		width: 100%;
		background-color: #fff;
		overflow: hidden
	}

	.uni-navbar__content view {
		line-height: 44px
	}

	.uni-navbar__header {
		display: flex;
		flex-direction: row;
		width: 100%;
		height: 44px;
		line-height: 44px;
		font-size: 16px
	}

	.uni-navbar__header-btns {
		display: inline-flex;
		flex-wrap: nowrap;
		flex-shrink: 0;
		width: 120upx;
		padding: 0 12upx
	}

	.uni-navbar__header-btns:first-child {
		padding-left: 0
	}

	.uni-navbar__header-btns:last-child {
		width: 60upx
	}

	.uni-navbar__header-container {
		width: 100%;
		margin: 0 10upx
	}

	.uni-navbar__header-container-inner {
		font-size: 30upx;
		text-align: center;
		padding-right: 60upx
	}

	.uni-navbar__placeholder-view {
		height: 44px
	}

	.uni-navbar--fixed {
		position: fixed;
		z-index: 998
	}

	.uni-navbar--shadow {
		box-shadow: 0 1px 6px #ccc
	}

	.uni-navbar--border:after {
		position: absolute;
		z-index: 3;
		bottom: 0;
		left: 0;
		right: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: #c8c7cc
	}
</style>

備註:左側使用click-left事件監聽,右側使用click-right事件監聽,中間部分可以配置title或者使用slot插槽,使用狀態欄組件來配置狀態欄。


第4步:在組件中引入導航欄組件。

<!-- 自定義導航欄 -->
<uni-nav-bar :statusBar="true" rightText="發佈" left-icon="back" @click-left="back" @click-right="submit">
	<!-- #ifdef APP-PLUS -->
	<view class="u-f-ajc" @tap="changeLook">
		{{yinsi}}
		<view class="icon iconfont icon-xialazhankai"></view>
	</view>
	<!-- #endif -->

	<!-- #ifdef H5 -->
	<view class="center-title">
		<view class="u-f-ajc" @tap="changeLook">
			{{yinsi}}
			<view class="icon iconfont icon-xialazhankai"></view>
		</view>
	</view>
	<!-- #endif -->
</uni-nav-bar>

備註:組件可以配置statusBar-是否添加狀態欄,rightText-右側文字,left-icon左側圖標,監聽click-left,監聽click-right,特別注意,APP端與H5端中間部分樣式有些許區別,需要條件編譯處理。


第五步:配置下拉菜單,並監聽點擊事件。

let changelook = ['所有人可見', '僅自己可見'];
import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue'

changeLook() {
	uni.showActionSheet({
		itemList:changelook ,
		success: (res) => {
			console.log(res);
			console.log('選中了第' + (res.tapIndex + 1) + '個按鈕');
			this.yinsi = changelook[res.tapIndex];
		},
		fail: (res) => {
			console.log(res.errMsg);
		}
	});
}

備註:在export外定義變量,不需要使用this指定。


效果圖:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章