微信小程序中實(shí)現(xiàn)自定義Navbar方法詳解
前言
自定義 navbar 應(yīng)該是很常見(jiàn)的需求。要自定義一個(gè) navbar 并不難,只需要了解其組成部分即可。

從上面的圖片可以看出,Navbar 由 StatusBar 和 TitleBar 組成。了解了這些組成部分之后,只需要知道它們各自的高度,就可以很好地完成自定義。
StatusBar高度
狀態(tài)欄高度與設(shè)備(即操作系統(tǒng))有關(guān)。在 uniapp 中,提供了一個(gè)名為 getSystemInfoSync 的方法,可以同步獲取與設(shè)備相關(guān)的內(nèi)容。
const systemInfo = uni.getSystemInfoSync(); const statusBarHeight = systemInfo.statusBarHeight;
TitleBar高度
通常,設(shè)備按照系統(tǒng)被劃分為 Android 和 iOS,這種分類(lèi)方式在行業(yè)內(nèi)是一種通用的標(biāo)準(zhǔn),雖然個(gè)別廠(chǎng)商可能會(huì)有一些細(xì)微的差異,但這里我們只關(guān)注通用標(biāo)準(zhǔn)。
const titleBarHeight = systemInfo.platform == 'android' ? 48 : 44;
知道 StatusBar 、TitleBar 高度之后,接下來(lái)定義就很簡(jiǎn)單了。
編寫(xiě)Navbar組件
<template>
<view class="navbar">
<!--statusBar-->
<view
v-if="showStatusBar"
class="status-bar"
:style="{ height: `${statusBarHeight}px` }"
></view>
<view class="navbar-content" :style="{ height: `${titleBarHeight}px` }">
<view class="navbar__left" @click="onClickLeft">
<view class="navbar__left-icon">
<slot name="left"></slot>
</view>
</view>
<view class="navbar__title">
<slot name="title">
{{ title }}
</slot>
</view>
<view class="navbar__right" @click="onClickRight">
<view class="navbar__right-icon">
<slot name="right"></slot>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
name: 'NavbarComponent',
props: {
title: {
type: String,
default: '',
},
showStatusBar: {
type: Boolean,
default: true,
},
},
setup(props, { emit, expose }) {
const systemInfo = uni.getSystemInfoSync();
const statusBarHeight = systemInfo.statusBarHeight;
const titleBarHeight = systemInfo.platform == 'android' ? 48 : 44;
const navbarHeight = statusBarHeight + titleBarHeight;
const getNavbarHeight = () => {
return props.showStatusBar ? navbarHeight : titleBarHeight;
};
const onClickLeft = () => {
emit('clickLeft');
};
const onClickRight = () => {
emit('clickRight');
};
expose({
getNavbarHeight,
});
return {
navbarHeight,
titleBarHeight,
statusBarHeight,
onClickLeft,
onClickRight,
};
},
};
</script>
<style scoped lang="scss">
.navbar {
padding: 0 20rpx;
.navbar-content {
display: flex;
align-items: center;
}
.navbar__title {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
flex: 1;
text-align: center;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.navbar__left {
flex: 0 0 80rpx;
display: flex;
align-items: center;
padding: 0 10rpx;
height: 100%;
font-size: 28rpx;
}
.navbar__right {
flex: 0 0 80rpx;
display: flex;
align-items: center;
padding: 0 10rpx;
height: 100%;
}
}
</style>使用
<template>
<b-navbar :title="title" @clickLeft="onClickLeft" @clickRight="onClickRight">
<template #left>
<uni-icons type="left"></uni-icons>
</template>
</b-navbar>
</template>
<script>
import { ref } from 'vue';
import BNavbar from '@/components/BNavbar/index.vue';
export default {
components: {
BNavbar,
},
setup() {
const title = ref('自定義Navbar');
const onClickLeft = () => {
uni.navigateBack();
};
const onClickRight = () => {
console.log('clickRight');
};
return {
title,
onClickLeft,
onClickRight,
};
},
};
</script>
<style scoped lang="scss">
.navbar {
display: flex;
align-items: center;
padding: 0 20rpx;
.navbar__title {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
flex: 1;
text-align: center;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.navbar__left {
flex: 0 0 80rpx;
display: flex;
align-items: center;
padding: 0 10rpx;
height: 100%;
font-size: 28rpx;
}
.navbar__right {
flex: 0 0 80rpx;
display: flex;
align-items: center;
padding: 0 10rpx;
height: 100%;
}
}
</style>最終效果

以上就是微信小程序中實(shí)現(xiàn)自定義Navbar方法詳解的詳細(xì)內(nèi)容,更多關(guān)于小程序自定義Navbar的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
微信小程序登錄數(shù)據(jù)解密及狀態(tài)維持實(shí)例詳解
這篇文章主要介紹了微信小程序登錄數(shù)據(jù)解密及狀態(tài)維持,結(jié)合實(shí)例形式分析了微信小程序解密敏感信息及獲取session保持登陸狀態(tài)的相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
JavaScript數(shù)組去重的五種方法及其他細(xì)節(jié)和拓展
JavaScript數(shù)組去重這個(gè)問(wèn)題,經(jīng)常出現(xiàn)在面試題中,下面這篇文章主要給大家介紹了關(guān)于JavaScript數(shù)組去重的五種方法及其他細(xì)節(jié)和拓展的相關(guān)資料,文中通過(guò)實(shí)例代碼以及圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
js jquery 獲取某一元素到瀏覽器頂端的距離實(shí)現(xiàn)方法
今天小編就為大家分享一篇js jquery 獲取某一元素到瀏覽器頂端的距離實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
JS實(shí)現(xiàn)圖片預(yù)加載無(wú)需等待
網(wǎng)站開(kāi)發(fā)時(shí)經(jīng)常需要在某個(gè)頁(yè)面需要實(shí)現(xiàn)對(duì)大量圖片的瀏覽;用javascript來(lái)實(shí)現(xiàn)一個(gè)圖片瀏覽器,讓用戶(hù)無(wú)需等待過(guò)長(zhǎng)的時(shí)間就能看到其他圖片2012-12-12
JavaScript實(shí)現(xiàn)時(shí)間范圍效果
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)時(shí)間范圍效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
JS實(shí)現(xiàn)簡(jiǎn)單的二維矩陣乘積運(yùn)算
這篇文章主要介紹了JS實(shí)現(xiàn)簡(jiǎn)單的二維矩陣乘積運(yùn)算方法,涉及JavaScript基于數(shù)組操作實(shí)現(xiàn)矩陣運(yùn)算的功能,需要的朋友可以參考下2016-01-01
javascript中createElement的兩種創(chuàng)建方式
這篇文章主要介紹了javascript中createElement的兩種創(chuàng)建方式,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05

