微信小程序自小程序自定义tabbar,不同页面显示不同tabbar

本文来源:微信小程序自定义底部导航栏,切换不同页面显示不同tabbar

微信小程序自定义不一样的tabBar

在一个微信小程序中想要用到两种不同的tabbar样式,需要在app.js中自定义,在页面加载时进行调用。

比如一个小程序需要两个版本(用户版、商家版),并且能通过一个按钮在两个版本间进行切换,可能会用到这种方式。

此处以两个页面(index,logs)显示两种tabbar样式为例,通过切换按钮进行切换。

首先有一个模板文件:tabbar.wxml

   

在app.json中无需定义“tabBar”
在app.js中自定义如下

//app.js  
App({
  onLaunch: function () {
 
  },
 
  //第一种底部  
  editTabBar: function () {
    //使用getCurrentPages可以获取当前加载中所有的页面对象的一个数组,数组最后一个就是当前页面。
 
    var curPageArr = getCurrentPages();    //获取加载的页面
    var curPage = curPageArr[curPageArr.length - 1];    //获取当前页面的对象
    var pagePath = curPage.route;    //当前页面url
    if (pagePath.indexOf('/') != 0) {
      pagePath = '/' + pagePath;
    }
    
    var tabBar = this.globalData.tabBar;
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (tabBar.list[i].pagePath == pagePath) {
        tabBar.list[i].active = true;    //根据页面地址设置当前页面状态    
      }
    }
    curPage.setData({
      tabBar: tabBar
    });
  },
  //第二种底部,原理同上
  editTabBar1: function () {
    var curPageArr = getCurrentPages();
    var curPage = curPageArr[curPageArr.length - 1];
    var pagePath = curPage.route;
    if (pagePath.indexOf('/') != 0) {
      pagePath = '/' + pagePath;
    }
    var tabBar = this.globalData.tabBar1;
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (tabBar.list[i].pagePath == pagePath) {
        tabBar.list[i].active = true; 
      }
    }
    curPage.setData({
      tabBar: tabBar
    });
  },
  globalData: {
    //第一种底部导航栏显示
    tabBar: {
      "color": "#9E9E9E",
      "selectedColor": "#f00",
      "backgroundColor": "#fff",
      "borderStyle": "#ccc",
      "list": [
        {
          "pagePath": "/pages/index/index",
          "text": "职位",
          "iconPath": "/images/my.png",
          "selectedIconPath": "/images/my.png",
          "clas": "menu-item",
          "selectedColor": "#4EDF80",
          active: true
        },
        {
          "pagePath": "/pages/logs/logs",
          "text": "简历",
          "iconPath": "/images/my.png",
          "selectedIconPath": "/images/my.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item",
          active: false
        },
        {
          "pagePath": "/pages/test/test",
          "text": "我的",
          "iconPath": "/images/my.png",
          "selectedIconPath": "/images/my.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item",
          active: false
        }
      ],
      "position": "bottom"
    },
    //第二种底部导航栏显示
    tabBar1: {
      "color": "#9E9E9E",
      "selectedColor": "#f00",
      "backgroundColor": "#fff",
      "borderStyle": "#ccc",
      "list": [
        {
          "pagePath": "/pages/index/index",
          "text": "首页",
          "iconPath": "/images/my.png",
          "selectedIconPath": "/images/my.png",
          "clas": "menu-item1",
          "selectedColor": "#4EDF80",
          active: false
        },
        {
          "pagePath": "/pages/logs/logs",
          "text": "消息",
          "iconPath": "/images/my.png",
          "selectedIconPath": "/images/my.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item1",
          active: true
        },
        {
          "pagePath": "/pages/cont/index",
          "text": "简历",
          "iconPath": "/images/my.png",
          "selectedIconPath": "/images/my.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item1",
          active: false
        },
        {
          "pagePath": "/pages/detail/index",
          "text": "我的",
          "iconPath": "/images/my.png",
          "selectedIconPath": "/images/my.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item1",
          active: false
        }
      ],
      "position": "bottom"
    }
  }
})  

在app.wxss中定义显示样式

.menu-item{  
  width: 32%;  
  float: left;  
  text-align: center;  
  padding-top: 8px;  
}  
.menu-item1{  
  width: 24%;  
  float: left;  
  text-align: center;  
  padding-top: 8px;  
}  
.img{ 
  width: 23px;  
  height: 23px;  
  display: block;  
  margin:auto;  
}  
.clear{  
  clear: both;  
}  
.tab-bar{  
  position: fixed;  
  width: 100%;  
  padding: 0px 2%;  
}  
 
.button{
  margin: 130px;
}

index.wxml,用到自定义tabbar的页面的首部都需要引入模板文件