uni-app自定義動態底部導航tabbar|仿微信tabbar導航欄,支持當前第幾個選中、自定義背景、圖片/圖標、字體顏色
uni-app原生導航tabBar功能也挺好,不過有時爲了知足多樣化的項目需求,如:背景色漸變、字體圖標,一些特殊圖標凸顯效果,這時候就只能自定義tabbar來實現功能了。
vue
以下圖:分別在H5端、小程序、APP端實現的自定義tabbar效果小程序
<block v-for="(item,index) in tabList" :key="index">
<view class="navigator" :class="currentTabIndex == index ? 'on' : ''" @tap="switchTab(index)">
<view class="icon">
<text class="iconfont" :class="item.icon" :style="[currentTabIndex == index ? {'color': tintColor} : {'color': color}]"></text>
<text v-if="item.badge" class="uni_badge">{{item.badge}}</text>
<text v-if="item.badgeDot" class="uni_badge uni_badge_dot"></text>
</view>
<view class="text" :style="[currentTabIndex == index ? {'color': tintColor} : {'color': color}]">{{item.text}}</view>
</view>
</block>
複製代碼
<script>
export default {
data() {
return {
tabList: [
{
icon: 'icon-emotion',
text: 'tab01',
badge: 1
},
{
icon: 'icon-qianbao',
text: 'tab02',
},
{
icon: 'icon-choose01',
text: 'tab03',
badgeDot: true
}
],
currentTabIndex: this.current
}
},
props: {
current: { type: [Number, String], default: 0 },
backgroundColor: { type: String, default: '#fbfbfb' },
color: { type: String, default: '#999' },
tintColor: { type: String, default: '#42b983' }
},
methods: {
switchTab(index){
this.currentTabIndex = index
this.$emit('click', index)
}
},
}
</script>
複製代碼
import tabBar from './components/tabbar.vue'
Vue.component('tab-bar', tabBar)
複製代碼
此時的tab-bar已是全局組件了,只需在須要的視圖頁面引用便可bash
<tab-bar :current="currentTabIndex" backgroundColor="#fbfbfb" color="#999" tintColor="#42b983" @click="tabClick"></tab-bar>
複製代碼
<tab-bar :current="2" backgroundColor="#1a4065" color="#c3daf1" tintColor="#02f32b" @click="tabClick"></tab-bar>
複製代碼
另外還支持點擊tabbar選項返回索引值微信
data() {
return {
...
currentTabIndex: 1
}
},
methods: {
tabClick(index){
console.log('返回tabBar索引:' + index)
this.currentTabIndex = index
},
...
},
複製代碼
最後附上uni-app自定義導航欄,但願能喜歡😎😎~~app
uniApp自定義頂部導航:juejin.im/post/5d806b…post