基于Vue3+UniApp在單個頁面實(shí)現(xiàn)固定TabBar的多種方式
在 UniApp 開發(fā)中,tabBar 通常是通過 pages.json 配置的,適用于整個應(yīng)用的全局導(dǎo)航
然而,在某些場景下,我們可能需要只在特定的頁面展示 tabBar,而不會影響其他頁面的布局。這就需要使用 自定義 tabBar,可以通過 view 組件、uni-segmented-control 組件或 uni-nav-bar 組件等方式來實(shí)現(xiàn)
以下是幾種適用場景:
局部頁面導(dǎo)航:
例如,一個訂單管理頁面,用戶可以在 “進(jìn)行中” 和 “已完成” 之間切換,而不影響全局 tabBar底部固定的二級導(dǎo)航:
例如,在 “記錄” 頁面,提供 “待審核” 和 “已審核” 選項(xiàng),方便用戶切換,而不會影響其他頁面頂部導(dǎo)航欄的切換:
適用于不適合使用底部 tabBar 的場景,如數(shù)據(jù)管理頁面,用戶可以在 “待審核” 和 “已審核” 之間切換
方法 | 適用場景 | 說明 |
---|---|---|
方法 1:自定義 view tabBar | 僅在某個頁面底部 | 適用于特定頁面,不會影響全局 tabBar |
方法 2:uni-segmented-control | 輕量級頁面切換 | 適用于簡單的 Tab 選項(xiàng),不會影響布局 |
方法 3:uni-nav-bar | 頂部導(dǎo)航切換 | 適用于帶頂部導(dǎo)航的 UI |
1. 自定義 view
<template> <view class="container"> <!-- 固定底部的 tabBar --> <view class="fixed-tabbar"> <view class="tab-item" :class="{ active: currentTab === 0 }" @click="currentTab = 0"> <text>測試A</text> </view> <view class="tab-item" :class="{ active: currentTab === 1 }" @click="currentTab = 1"> <text>測試B</text> </view> </view> </view> </template> <script> export default { data() { return { currentTab: 0, // 當(dāng)前選中的 tab }; } }; </script> <style scoped> .container { display: flex; flex-direction: column; height: 100vh; } .content { flex: 1; overflow-y: auto; /* 讓內(nèi)容可以滾動 */ padding-bottom: 60px; /* 避免被底部 tabBar 遮擋 */ } .fixed-tabbar { position: fixed; bottom: 0; left: 0; width: 100%; height: 50px; display: flex; justify-content: space-around; align-items: center; background-color: #fff; border-top: 1px solid #ddd; } .tab-item { flex: 1; text-align: center; padding: 10px; font-size: 14px; color: #666; } .tab-item.active { color: #007AFF; font-weight: bold; } </style>
截圖如下:
fixed-tabbar 采用 position: fixed; bottom: 0;,始終固定在頁面底部
使用 currentTab 變量控制當(dāng)前選中的 tab,并根據(jù) active 類名高亮
padding-bottom: 60px; 避免頁面內(nèi)容被 tabBar 遮擋
2. uni-segmented-control
以輕量級方式切換不同的頁面內(nèi)容,不需要固定的底部 tabBar
<template> <view> <!-- 頂部 Tab 切換 --> <uni-segmented-control :current="currentTab" :values="['測試A', '測試B']" @clickItem="switchTab" /> <!-- 提柜記錄 --> <view v-if="currentTab === 0"> <uni-card title="測試A"> <text>這里是測試A...</text> </uni-card> </view> <!-- 殘損單記錄 --> <view v-else> <uni-card title="測試B"> <text>這里是測試B...</text> </uni-card> </view> </view> </template> <script> export default { data() { return { currentTab: 0 // 默認(rèn)選中第一個 }; }, methods: { switchTab(index) { this.currentTab = index; } } }; </script>
截圖如下:
uni-segmented-control 提供頂部 tab,點(diǎn)擊時 @clickItem 觸發(fā) switchTab 進(jìn)行切換
僅 currentTab === 0 時展示
適用于無需固定底部導(dǎo)航欄的場景,如數(shù)據(jù)篩選切換
3. uni-nav-bar
頂部導(dǎo)航,在標(biāo)題欄左右提供不同的 Tab 切換按鈕
<template> <view> <!-- 自定義頂部 Tab --> <uni-nav-bar > <template v-slot:left> <view @click="switchTab(0)" :class="{ active: currentTab === 0 }">測試A</view> </template> <template v-slot:right> <view @click="switchTab(1)" :class="{ active: currentTab === 1 }">測試B</view> </template> </uni-nav-bar> <!-- 頁面內(nèi)容 --> <view v-if="currentTab === 0"> <uni-card title="測試A"> <text>這里是測試A...</text> </uni-card> </view> <view v-else> <uni-card title="測試B"> <text>這里是測試B...</text> </uni-card> </view> </view> </template> <script> export default { data() { return { currentTab: 0 }; }, methods: { switchTab(index) { this.currentTab = index; } } }; </script> <style scoped> .active { font-weight: bold; color: #007AFF; } </style>
uni-nav-bar 作為導(dǎo)航欄,在左右 slot 里添加點(diǎn)擊切換的按鈕。
switchTab 控制當(dāng)前顯示的內(nèi)容
以上就是基于Vue3+UniApp在單個頁面實(shí)現(xiàn)固定TabBar的多種方式的詳細(xì)內(nèi)容,更多關(guān)于Vue3 UniApp固定TabBar的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于VanCascader默認(rèn)值(地址code轉(zhuǎn)換)
這篇文章主要介紹了關(guān)于VanCascader默認(rèn)值(地址code轉(zhuǎn)換),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07解決vue+router路由跳轉(zhuǎn)不起作用的一項(xiàng)原因
這篇文章主要介紹了解決vue+router路由跳轉(zhuǎn)不起作用的一項(xiàng)原因,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue測試環(huán)境打包與生產(chǎn)環(huán)境打包文件數(shù)量不一致解決方案
這篇文章主要為大家介紹了vue測試環(huán)境打包與生產(chǎn)環(huán)境打包文件數(shù)量不一致的解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Vue+Element-ui表單resetFields無法重置問題
本文主要介紹了Vue+Element-ui表單resetFields無法重置問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04vue2+springsecurity權(quán)限系統(tǒng)的實(shí)現(xiàn)
本文主要介紹了vue2+springsecurity權(quán)限系統(tǒng)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Vue 3.0 前瞻Vue Function API新特性體驗(yàn)
這篇文章主要介紹了Vue 3.0 前瞻Vue Function API新特性體驗(yàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08