微信小程序之tabBar自定義及切換不同版本顯示不同tabBar

今天,我在寫頁面的時候,碰到需要切換不同身份(兩個版本)時候底部tabBar顯示不同,我的解決方法是:

首先在components(組件模板)下新建一個tabBar.wxml:

<template name="tabBar">    
  <view class="tab-bar" style="color: {{tabBar.color}}; background: {{tarBar.backgroundColor}}; {{tabBar.position=='top'? 'top: 0' : 'bottom: 0'}}; {{tabBar.borderStyle? (tabBar.position=='top'? 'border-bottom: solid 1px '+tabBar.borderStyle + ';' : 'border-top: solid 1px '+tabBar.borderStyle + ';') : ''}}">    
  <block wx:for="{{tabBar.list}}" wx:key="pagePath">    
    <navigator url="{{item.pagePath}}" open-type="redirect" class="{{item.clas}}" style="{{item.active? 'color: '+(item.selectedColor? item.selectedColor : tabBar.selectedColor) : ''}}">    
      <image src="{{item.selectedIconPath}}" wx:if="{{item.active}}" class="tab-img"></image>    
      <image src="{{item.iconPath}}" wx:if="{{!item.active}}" class="tab-img"></image>  
      <text>{{item.text}}</text>    
    </navigator>    
    </block>     
  </view>    
</template>   

然後在app.js中

 globalData: {
    //版本1底部導航欄顯示
    tabBar: {
      "color": "#9E9E9E",
      "list": [
        {   
          "pagePath": "/pages/student/student",
          "text": "服務",
          "iconPath": "../../images/fuwu.png",
          "selectedIconPath": "../../images/fuwu-active.png",
          "clas": "tab-item",
          "selectedColor": "#4EDF80",
          active: true
        },
        {
          "pagePath": "/pages/mine/mine",
          "text": "我的",
          "iconPath": "../../images/mine.png",
          "selectedIconPath": "../../images/mine-active.png",
          "selectedColor": "#4EDF80",
          "clas": "tab-item",
          active: false
        }
      ],
      "position": "bottom"
    },
    //版本2底部導航欄顯示
    tabBar1: {
      "color": "#9E9E9E",
      "list": [   
        {
          "pagePath": "/pages/teacher/teacher",
          "text": "服務",
          "iconPath": "../../images/fuwu.png",
          "selectedIconPath": "../../images/Tfuwu-active.png",
          "clas": "tab-item",
          "selectedColor": "#2180ed",
          active: true
        },
        {
          "pagePath": "/pages/Tmine/Tmine",
          "text": "我的",
          "iconPath": "../../images/mine.png",
          "selectedIconPath": "../../images/Tmine-active.png",
          "selectedColor": "#2180ed",
          "clas": "tab-item",
          active: false
        }
      ],
      "position": "bottom"
    }
  },

接着app.wxss

.tab-item {
  padding: 0 20rpx;
  text-align: center;
  padding-top: 8px;
}

.tab-img {
  width: 23px;
  height: 23px;
  display: block;
  margin: auto;
}

.tab-bar {
  position: fixed;
  width: 100%;
  background-color: #fff;
  z-index: 100;
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
}

寫好了兩個不同的底部導航的樣式後,下一步就是在你需要頁面中如何引用了。

我這裏根據我的需求在四個頁面中做了引用,例如:student.wxml

<!-- 引入底部導航欄 -->
<import src="../../components/tabBar.wxml" />
<template is="tabBar" data="{{tabBar}}" />

然後是在對應的js中加入兩句話,這裏對應的js頁面是student.js,首先要記得在頂部寫上var app = getApp();,然後在onload(),或者onshow()方法中添加上

   onLoad: function (options) {
    app.editTabBar();    //顯示自定義的底部導航
  },

到這裏就搞定了自定義tabBar的使用了。

效果圖如下:
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

這裏要注意,如果你要引用另一個版本的底部TabBar,只需注意js中的onload()或者onshow()在這裏插入圖片描述

好了,以上就是我的分享了,有更好的方法可以評論區告訴我。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章