Vue实现自定义tabbar

<template>
  <div class="tabbar footer">
     <div class="tabbar-item" v-for="(item,index) in tabbarList" :key="index" @click="switchTab(item)">
       <div class="tab_icon">
         <img
           :src="item.pageUrl == $route.path?item.activeImg:item.normalImg"
           width="48px"
           alt=""
         />
         <!-- 气泡 -->
         <div class="msg" v-if="item.isShowBubble">
          99
         </div>
       </div>
       <h3 :class="[item.pageUrl == $route.path?textStyleLight:textStyleDark]">{{item.text}}</h3>
     </div>
   </div>
</template>
<script>

export default {
  data () {
    return {
      textStyleLight : 'textStyleLight',
      textStyleDark : 'textStyleDark',
      tabbarList : [
            {
                text : '首页',
                pageUrl : '',
                activeImg : '',
                disactiveImg : '',
                isShowBubble : false
            },
            {
                text : '权益中心',
                pageUrl : '',
                activeImg : '',
                disactiveImg : '',
                isShowBubble : false
            },
            {
                text : '我的',
                pageUrl : '',
                activeImg : '',
                disactiveImg : '',
                isShowBubble : false
            }
        ],
    }
  },
  methods: {
    switchTab (res) {
      if( res.pageUrl == this.$route.path ) return;
      this.$router.push(res.pageUrl)
    }
  }
}
</script>
<style lang="less">
.tabbar {
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 100;
  width: 100%;
  padding: .04rem .36rem;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0px 0px 4px 0px rgba(0, 0, 0, 0.1);
  background-color: #fff;
  .tabbar-item {
    display: flex;
    flex-direction: column;
    text-align: center;
    .tab_icon{
      position: relative;
      .msg{
        position: absolute;
        top: 0;
        left: 65%;
        width: 0.18rem;
        height: 0.18rem;
        border-radius: 50%;
        text-align: center;
        font: 400 0.13rem PingFangSC-Semibold, PingFang SC;
        color: #fff;
        background-color: #ff4668;
      }
    }
    .textStyleLight,.textStyleDark {
      margin: 0;
      font: 600 0.12rem PingFangSC-Semibold, PingFang SC;
    }
    .textStyleLight {
      color: #4865ff;
    }
    .textStyleDark{
      color: #999;
    }
  }
}
// 适配iPhone X以上机型底部栏
@supports (bottom: env(safe-area-inset-bottom)){
    body,
    .footer{
        padding-bottom: constant(safe-area-inset-bottom);
        padding-bottom: env(safe-area-inset-bottom);
    }
}
</style>
### 实现 UniApp 中 H5 平台自定义底部 TabBar #### 配置 manifest.json 文件 为了使自定义 tabBar 生效,在 `manifest.json` 的 `"app-plus"` 字段下设置 `"usingComponents"` 和 `"custom-tab-bar"` 属性。对于 H5 平台而言,确保 `"pages"` 数组中的首页路径已经指定好,并且该页面配置了 `"style"` 对象下的 `"navigationStyle"` 为 `"custom"` 或者其他非默认值以便于控制顶部导航条风格[^1]。 ```json { "path": "pages/index/index", "style": { "navigationBarTitleText": "Home Page", "navigationStyle": "custom" } } ``` #### 创建自定义组件 建立一个新的 Vue 组件文件用于表示自定义tabBar。此组件可以放置在项目的 `/components/Tabbar/Tabbar.vue` 路径下。这个组件应该包含所有的 HTML 结构、CSS 样式以及交互逻辑来构建所需的 tabBar 外观和行为[^3]。 ```html <template> <view class="tab-bar"> <!-- 自定义 tabBarItem --> <block v-for="(item, index) in items" :key="index"> <view @click="switchPage(item.pagePath)" :class="{ active: item.selected }">{{ item.text }}</view> </block> <!-- 特殊处理中间按钮 --> <button type="primary" size="mini">+</button> </view> </template> <script> export default { data() { return { items: [ { text: '主页', pagePath: '/pages/home/home', selected: false }, { text: '分类', pagePath: '/pages/category/category', selected: false }, // ... 更多选项 ... ] }; }, methods: { switchPage(path) { uni.switchTab({ url: path }); } } }; </script> <style scoped> .tab-bar { display: flex; justify-content: space-around; align-items: center; } .active { color: blue; } </style> ``` #### 使用自定义组件并初始化状态 最后一步是在应用的主要布局模板里引入刚才创建好的 tabBar 组件,并通过 JavaScript 初始化当前选中的 tabItem 状态。注意这里针对非微信小程序环境调用了 `uni.hideTabBar()` 方法以移除默认显示出来的 tabBar[^2]。 ```javascript import TabBar from '@/components/Tabbar/Tabbar.vue'; export default { components: { TabBar }, setup(props) { const currentItem = ref(null); onMounted(() => { currentItem.value = props.currentPage; if (process.env.VUE_APP_PLATFORM !== 'mp-weixin') { uni.hideTabBar(); } return { currentItem }; }); return {}; } }; <template> <view> <slot /> <TabBar></TabBar> </view> </template> ``` 以上就是在 UniApp H5 应用程序中实现自定义底部 tabBar 所需的关键步骤和技术要点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值