自定义 uni-app 的 tabbar底部导航

4 篇文章 0 订阅
1 篇文章 0 订阅

uni-app中原生tabbar配置和小程序差不多,具体配置查看这篇文章
https://uniapp.dcloud.io/collocation/pages?id=tabbar

"tabBar": {
    "color": "#7A7E83",
    "selectedColor": "#3cc51f",
    "borderStyle": "black",
    "backgroundColor": "#ffffff",
    "list": [{
        "pagePath": "pages/component/index",
        "iconPath": "static/image/icon_component.png",
        "selectedIconPath": "static/image/icon_component_HL.png",
        "text": "组件"
    }, {
        "pagePath": "pages/API/index",
        "iconPath": "static/image/icon_API.png",
        "selectedIconPath": "static/image/icon_API_HL.png",
        "text": "接口"
    }]
}

在pages.json中如果没有配置tabBar参数,uni-app中就不会显示底部tabbar,可以通过自定义组件形式来实现想要的自定义tabbar效果。
如下图:在H5端、小程序、App端 下显示的自定义tabbar效果
在这里插入图片描述
新建tabbar.vue组件,并在main.js里面引入全局组件

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

在页面中引入tabbar,并自定义tabbar属性参数
在这里插入图片描述

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

在这里插入图片描述

data() {
    return {
        ...
        currentTabIndex: 1
    }
},
methods: {
    tabClick(index){
        console.log('返回tabBar索引:' + index)
        this.currentTabIndex = index
    },
},

上图实例中uni-app自定义顶部导航栏

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-app是一个跨平台的开发框架,可以用于开发多个平台的应用程序。在uni-app中,可以通过自定义底部导航栏来实现个性化的界面设计和功能需求。 以下是一种实现自定义底部导航栏的方法: 1. 首先,创建一个自定义组件,用于显示底部导航栏的每个按钮。可以使用`uni-icons`组件来显示图标,并添加相应的点击事件。 ```vue <template> <view> <view class="tabbar"> <view class="tabbar-item" @click="navigateTo('home')"> <uni-icons type="home"></uni-icons> <text>首页</text> </view> <view class="tabbar-item" @click="navigateTo('discover')"> <uni-icons type="discover"></uni-icons> <text>发现</text> </view> <view class="tabbar-item" @click="navigateTo('mine')"> <uni-icons type="mine"></uni-icons> <text>我的</text> </view> </view> </view> </template> <script> export default { methods: { navigateTo(page) { uni.navigateTo({ url: `/pages/${page}/${page}` }); } } } </script> <style> .tabbar { display: flex; justify-content: space-around; align-items: center; height: 100px; background-color: #f5f5f5; } .tabbar-item { display: flex; flex-direction: column; align-items: center; } </style> ``` 2. 在需要显示底部导航栏的页面中,引入自定义组件,并在页面的底部位置使用该组件。 ```vue <template> <view> <!-- 页面内容 --> </view> <custom-tabbar></custom-tabbar> </template> <script> import CustomTabbar from '@/components/CustomTabbar.vue'; export default { components: { CustomTabbar } } </script> ``` 通过以上步骤,你可以实现一个自定义底部导航栏,并在不同的页面之间进行切换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值