Implementation of uni app Custom Bottom Navigation Bar

  • 2021-10-13 06:24:32
  • OfStack

This is the better uni-app custom bottom navigation bar method I have found so far. The main disadvantage of other methods is that when switching, it will either flash or request data once every click. If there are other better ways, please comment. I only started writing projects with uni-app recently. I just looked at the documentation before.

1. tabbar component


<template>
 <view class="tabbar-container">
  <view
   :style="{ color: currentIndex == index ? '#007EFF' : '#333333' }"
   v-for="(item, index) in tabbarList"
   :key="index"
   style="flex: 1"
   @click="switchTab(index)"
  >
   <view :class="'iconfont ' + item.icon" />
   <view class="title">{{ item.title }}</view>
  </view>
 </view>
</template>

mounted(){
 let dom = uni.createSelectorQuery().select('.tabbar-container')
  dom.boundingClientRect(e => {
   // tabbarHeight If it is used frequently, it will be set as a global variable 
    getApp().globalData.tabbarHeight = e.height
  }).exec()
}

<style scoped lang="scss">
.iconfont {
 font-size: 18px;
}

.tabbar-container {
 display: flex;
 justify-content: space-evenly;
 text-align: center;
 padding: 10px 0;
 background-color: #fff;
 box-shadow: 0 -1.5px 3px #eee;
 z-index: 999;

 .title {
  font-size: 12px;
 }
}
</style>

2. Introduce

swiper is used here, and duration is 0 to turn off the animation effect of page switching.


<template>
 <view :style="'height: calc(100vh - ' + tabbarHeight + 'px)'">
  <tab-bar
   :currentIndex="currentIndex"
   class="tabbar-container"
   @getCurrentIndex="getCurrentIndex"
  />
  <swiper duration="0" disable-touch :current="currentIndex" style="height: 100%">
   <swiper-item>
    <scroll-view scroll-y style="height: 100%">
     <home />
    </scroll-view>
   </swiper-item>
   <swiper-item>
    <todo-page />
   </swiper-item>
   <swiper-item>
    <launch-task />
   </swiper-item>
   <swiper-item>
    <my-page />
   </swiper-item>
  </swiper>
 </view>
</template>

mounted() {
 this.tabbarHeight = getApp().globalData.tabbarHeight
},

getCurrentIndex(e) {
 this.currentIndex = e;
}

Related articles: