x***@126.com
x***@126.com
  • 发布:2019-09-24 01:07
  • 更新:2024-03-27 09:25
  • 阅读:21737

uniapp之tabBar自定义底部导航

分类:uni-app

uni-app原生导航tabBar功能也挺好,不过有时为了满足多样化的项目需求,如:背景色渐变、字体图标,一些特殊图标凸显效果,这时候就只能自定义tabbar来实现功能了。
如下图:分别在H5端、小程序、APP端实现的自定义tabbar效果

新建tabbar.vue组件

<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>

在main.js里面引入全局组件

import tabBar from './components/tabbar.vue'  
Vue.component('tab-bar', tabBar)

此时的tab-bar已经是全局组件了,只需在需要的视图页面引用即可

<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>


最后附上uni-app自定义导航栏,希望能喜欢~~
uni-app自定义顶部导航器:https://ask.dcloud.net.cn/article/36388

2 关注 分享
flyer456 chenguokai

要回复文章请先登录注册

x***@126.com

x***@126.com (作者)

所有插件开源了,一次性拿走使用。
[https://ext.dcloud.net.cn/publisher?id=90611](https://ext.dcloud.net.cn/publisher?id=90611)
2024-03-27 09:25
x***@126.com

x***@126.com (作者)

如果有需要,来我工房看看吧。
[https://gf.bilibili.com/item/detail/1105131011](https://gf.bilibili.com/item/detail/1105131011)
2024-03-27 09:22
狂人的芝士

狂人的芝士

刚学uni-app,也知道迟早有一天需要用到自定义的tabbar所以来看看,想知道如何实现这个tabbar一直固定在底部,切换页面只是上半部分的页面之间切换,tabbar只是变化图标颜色而已?
web端用vue的话很好实现,有<router-view>标签,这个tabbar写在app.vue中即可,但是uni-app中该咋写呀,小白一时想不明白。。
2021-06-21 22:00
HZH8997

HZH8997

大佬,请问一下,切换页面时,底部导航闪烁怎么处理呢
2021-01-12 17:43
晚媚

晚媚

还有个疑问,怎么设置APP初始进入页呢?如果用这种自定义的方式控制显示的tab个数,入口页可能都不一样,这个怎么解决呀
2020-11-10 10:38
x***@163.com

x***@163.com

回复 ZHANGHANG :
可以试着把所有页面放一个里,只是切换显示隐藏,但是初次加载会慢,性能可能一般
2020-09-15 11:53
ZHANGHANG

ZHANGHANG

怎么解决 在小程序端点击tabbar切换页面的时候tabbar会重新渲染?
2020-02-14 10:47