【uni-app】tabbar自定义

需求

自带的tabbar是无法做成圆角加阴影的,最后的解决方法只能是自定义tabbar。

实现

第一步:在page.json中定义两个tab的路径。

"tabBar": {
  "blurEffect":"extralight",
  "color": "#909399",
  "selectedColor": "#18b566",
  "borderStyle": "black",
  "list": [
    {
      "pagePath": "pages/list/list"
    },
    {
      "pagePath": "pages/my/my"
    }
  ]
},

第二步:在components中自定义一个tabbar的组件

<template>
  <view class="tabbar" >
    <view class="tabbar-item" v-for="(item,index) in list" :key="index" @click="changeTab(index)">
      <image class="img" :src="item.selectedIconPath" v-if="current == index"></image>
      <image class="img" :src="item.iconPath" v-else></image>
      <view class="text active" v-if="current == index">{{item.text}}</view>
      <view class="text" v-else>{{item.text}}</view>
    </view>
  </view>
</template>
 
<script>
export default {
    name: "tabbar",
    props: {
      current: Number
    },
    created() {
      uni.hideTabBar() 
    },
    data() {
      return {
        list: [
          {
            selectedIconPath: "../../static/tabbar/list-active.png",
            iconPath: "../../static/tabbar/list.png",
            text: '列表',
            pagePath: "pages/list/list"
          },
          {
            selectedIconPath: "../../static/tabbar/my-active.png",
            iconPath: "../../static/tabbar/my.png",
            text: '我的',
            pagePath: "pages/my/my"
          }
        ],
      }
    },
    methods: {
      changeTab(e) {
        uni.switchTab({
          url: '/' + this.list[e].pagePath,
        })
      }
    }
}
</script>
 
<style scoped>
  .tabbar {
    position: fixed;
    left: 0;
    bottom: 0;
    z-index: 99;
    width: 100%;
    height: 150upx;
    display: flex;
    align-items: center;
    justify-content: space-around;
    background-image:url(../../static/tabbar/bg.png);
    background-repeat: no-repeat;
    background-size: 100% 100%;
    padding: 30rpx 0 0;
  },
  .tabbar-item {
    padding: 0 40rpx;
  }
  .img {
    height: 42upx;
    width: 42upx;
  }
  .text {
    font-size: 23upx;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    color: #CACACA;
    line-height: 27upx;
  }
  .text.active {
    color: #409EFE;
  }
</style>

第三步:使用tabbar。

<!-- list页面 -->
<tabbar current="0"></tabbar>
<!-- my页面 -->
<tabbar current="1"></tabbar>

<script>

import tabbar from '@/components/tabbar/index'
components:{
  tabbar
}

</script>

解析

进入页面就隐藏tabbar

created() {
  uni.hideTabBar() 
},

tabbar遮住正常页面,层级又不能高于遮罩层,设置层级99。

.tabbar {
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 99;
}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
UniApp中,可以通过自定义底部TabBar来实现自定义的底部导航栏。以下是一些步骤来实现自定义底部TabBar: 1. 创建一个底部TabBar组件:在components文件夹中创建一个名为TabBar的组件,用于显示底部导航栏的内容。 2. 在App.vue中引入TabBar组件:在App.vue文件中引入TabBar组件,并在页面中使用该组件。 3. 定义底部导航栏的数据:在TabBar组件中定义一个数组,包含每个底部导航栏的图标、文字等信息。 4. 使用循环渲染底部导航栏:在TabBar组件的template中使用v-for指令,循环渲染每个底部导航栏项。 5. 添加点击事件:为每个底部导航栏项添加点击事件,用于切换页面或执行其他操作。 6. 在页面中使用自定义底部TabBar:在需要显示底部导航栏的页面中,使用自定义的底部TabBar组件。 以下是一个简单的示例代码: ```vue <!-- TabBar.vue --> <template> <view class="tab-bar"> <view v-for="(item, index) in tabBarList" :key="index" class="tab-bar-item" :class="{ active: activeIndex === index }" @click="handleTabClick(index)" > <image :src="item.icon" class="tab-bar-icon" /> <text class="tab-bar-text">{{ item.text }}</text> </view> </view> </template> <script> export default { data() { return { tabBarList: [ { icon: 'home.png', text: '首页' }, { icon: 'category.png', text: '分类' }, { icon: 'cart.png', text: '购物车' }, { icon: 'user.png', text: '我的' }, ], activeIndex: 0, }; }, methods: { handleTabClick(index) { this.activeIndex = index; // 执行切换页面或其他操作 }, }, }; </script> <!-- App.vue --> <template> <view class="app"> <router-view /> <TabBar /> </view> </template> <script> import TabBar from '@/components/TabBar.vue'; export default { components: { TabBar, }, }; </script> ``` 在以上示例中,TabBar组件循环渲染了底部导航栏的每个项,并通过点击事件切换页面或执行其他操作。在App.vue中使用了TabBar组件,并在页面中显示底部导航栏。 你可以根据自己的需求,对TabBar组件进行样式和功能的定制,以满足自定义底部TabBar的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宾果的救星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值