微信小程序中實(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
,這種分類方式在行業(yè)內(nèi)是一種通用的標(biāo)準(zhǔn),雖然個(gè)別廠商可能會(huì)有一些細(xì)微的差異,但這里我們只關(guān)注通用標(biāo)準(zhǔn)。
const titleBarHeight = systemInfo.platform == 'android' ? 48 : 44;
知道 StatusBar
、TitleBar
高度之后,接下來(lái)定義就很簡(jiǎn)單了。
編寫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)文章
JavaScript資源預(yù)加載組件和滑屏組件的使用推薦
這篇文章主要介紹了JavaScript資源預(yù)加載組件和滑屏組件的使用推薦,分別為preload和slide的用法講解,使用起來(lái)非常簡(jiǎn)單,需要的朋友可以參考下2016-03-03js中的值類型和引用類型小結(jié) 文字說(shuō)明與實(shí)例
下面就舉例講一下這兩種類型在JavaScript中的體現(xiàn)、用法及注意事項(xiàng)。2010-12-12深入解析Javascript閉包的功能及實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)解析Javascript閉包的功能及實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-07-07測(cè)量JavaScript函數(shù)的性能各種方式對(duì)比
這篇文章主要介紹了測(cè)量JavaScript函數(shù)的性能各種方式對(duì)比,對(duì)性能感興趣的同學(xué),可以多實(shí)驗(yàn)一下2021-04-04TypeScript?Pinia實(shí)戰(zhàn)分享(Vuex和Pinia對(duì)比梳理總結(jié))
這篇文章主要介紹了TypeScript?Pinia實(shí)戰(zhàn)分享(Vuex和Pinia對(duì)比梳理總結(jié)),今天我們?cè)賮?lái)實(shí)戰(zhàn)下官方推薦的新的vue狀態(tài)管理工具Pini,感興趣的小伙伴可以參考一下2022-06-06基于JavaScript實(shí)現(xiàn)添加到購(gòu)物車效果附源碼下載
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn)添加到購(gòu)物車效果附源碼下載的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08