<view bindtap=‘tabBarFun‘ class=‘item_con‘ id="{{item.id}}" wx:for="{{tabBarList}}" wx:key="id">
<image wx:if="{{!item.isCouter}}" class="{{item.class}}" src="{{item.iconPath}}" />
<image wx:if="{{item.isCouter}}" class="{{item.class}}" src="{{item.selectedIconPath}}" />
<view class="{{item.isCouter?‘tab_font isCouter‘:‘tab_font‘}}">{{item.text}}</view>
const app = getApp();
Component({
data: {
isModal: true,
tabBarList: [
{
pagePath: "pages/index/index",
text: "首页",
id: 1,
isCouter: true,
class: "tab_logo",
color: "#888888",
iconPath: "../../icon/indexIcon.png",
selectedColor: "#2873ff",
selectedIconPath: "../../icon/indexIconSe.png",
link: ‘../index/index‘
},
{
pagePath: "pages/mall/mall",
text: "商城",
class: "tab_logo",
id: 2,
isCouter: false,
iconPath: "../../icon/selectCarIcon.png",
selectedIconPath: "../../icon/selectCarIconSe.png",
link: ‘../mall/mall‘
},
{
pagePath: "pages/new/new",
class: "logo_mid",
id: 5,
isCouter: true,
iconPath: "../../icon/tabBar_new.png",
selectedIconPath: "../../icon/tabBar_new.png"
},
{
pagePath: "pages/message/message",
text: "消息",
class: "tab_logo",
id: 3,
isCouter: false,
iconPath: "../../icon/consultingIcon.png",
selectedIconPath: "../../icon/consultingIconSe.png",
link: ‘../message/message‘
},
{
pagePath: "pages/mine/mine",
text: "我的",
id: 4,
isCouter: false,
class: "tab_logo",
color: "#888888",
selectedColor: "#2873ff",
iconPath: "../../icon/myIcon1.png",
selectedIconPath: "../../icon/myIconSe.png",
link: ‘../mine/mine‘
}
]
},
ready: function(options) {
var ts = this;
var pages = getCurrentPages();
    var currentPage = pages[pages.length - 1]; //获取当前页面的对象
    var url = currentPage.route; //当前页面url
    var list = this.data.tabBarList;
    if (url == "pages/index/index") {
      list[0].isCouter = true;
      list[1].isCouter = false;
      list[4].isCouter = false;
      list[3].isCouter = false;
      this.setData({
        tabBarList: list,
      });
      //debugger
    } else if (url == "pages/mall/mall") {
      list[1].isCouter = true;
      list[0].isCouter = false;
      list[4].isCouter = false;
      list[3].isCouter = false;
      this.setData({
        tabBarList: list,
      });
    } else if (url == "pages/message/message") {
      list[3].isCouter = true;
      list[4].isCouter = false;
      list[1].isCouter = false;
      list[0].isCouter = false;
      this.setData({
        tabBarList: list,
      });
    } else if (url == "pages/mine/mine") {
      list[4].isCouter = true;
      list[0].isCouter = false;
      list[1].isCouter = false;
      list[3].isCouter = false;
      this.setData({
        tabBarList: list,
      });
    }
  },
  methods: {
    hideToCreate: function () {
      this.setData({
        isModal: true,
      });
    },
    toNewPost: function () {
      wx.navigateTo({
        url: ‘../index/newPost/newPost‘
      });
      this.setData({
        isModal: true,
      });
    },
    toNewDynamic: function () {
      wx.navigateTo({
        url: "../index/newDynamic/newDynamic"
      });
      this.setData({
        isModal: true,
      });
    },
    tabBarFun: function(e) {
      var ts = this;
      var id = Number(e.currentTarget.id);
      var list = this.data.tabBarList;
      var pages = app.pages;
      console.log(‘length:‘+pages.length)
      var currentPage = pages[pages.length - 1]; //获取当前页面的对象
      var url = currentPage.route; //当前页面url
      let len = 0,
        flag = false;
     // debugger
      if (url != "pages/index/index" && id == 1) {
        for (let i = 0; i < pages.length; i++) {
          ++len;
          if (pages[i].route == "pages/index/index") {
            flag = true;
            break;
          }
        }
        console.log(‘len:index‘+len);
        if (flag) {
          wx.navigateBack({
            delta: pages.length - len
          });
        } else {
          wx.navigateTo({
            url: ‘pages/index/index‘
          });
        }
      } else if (url != "pages/mall/mall" && id == 2) {
        for (let i = 0; i < pages.length; i++) {
          ++len;
          if (pages[i].route == "pages/mall/mall") {
            flag = true;
            break;
          }
        }
        if (flag) {
          wx.navigateBack({
            delta: pages.length - len
          });
        } else {
          wx.navigateTo({
            url: ‘/pages/mall/mall‘
          });
        }
      } else if (url != "pages/message/message" && id == 3) {
        for (let i = 0; i < pages.length; i++) {
          ++len;
          if (pages[i].route == "pages/message/message") {
            flag = true;
            break;
          }
        }
        if (flag) {
          wx.navigateBack({
            delta: pages.length - len
          });
        } else {
          wx.navigateTo({
            url: ‘/pages/message/message‘
          });
        }
      } else if (url != "pages/mine/mine" && id == 4) {
        for (let i = 0; i < pages.length; i++) {
          ++len;
          if (pages[i].route == "pages/mine/mine") {
            flag = true;
            break;
          }
        }
        if (flag) {
          wx.navigateBack({
            delta: pages.length - len
          });
        } else {
          wx.navigateTo({
            url: ‘/pages/mine/mine‘
          });
        }
      } else if (id == 5) {
        this.setData({
          isModal: !this.data.isModal,
        })
      } 
    }
  }
})
 
4.tabBar.json
 
{
"component": true
}
 
5.tabBar.wxss
 
.wrapper{
height: 100rpx;
}
.item_con {
width: 20%;
float: left;
text-align: center;
height: 100rpx;
color: #444;
}
.modal_bg {
background-color:
rgba(100, 100, 100, 0.6);
 
  position: fixed;
  width: 100%;
  height: 100%;
}
.barModal {
background-color:
rgba(100, 100, 100, 0.6); 
  position: fixed;
  width: 100%;
  height: 100%;
  left: 0;
  z-index: 1;
  top: 0;
}
.inte_ite {
  position: absolute;
  bottom: 100rpx;
  left: 0;
  width: 100%;
}
.toImgbar {
  width: 80rpx;
  height: 80rpx;
}
.inte_ite {
  ">#fff;
  padding: 10rpx;
}
.tobarItem {
  width: 50%;
  font-size: 28rpx;
  float: left;
  text-align: center;
}
.wrappe_tab {
  position: fixed;
  left: 0;
  z-index: 2;
  height: 100rpx;
  width: 100%;
  border-top: 1rpx solid #eee;
  bottom: 0;
  ">#fff;
}
.tab_logo {
  width: 45rpx;
  height: 38rpx;
  margin-top: 15rpx;
}
.tab_font {
  font-size: 28rpx;
  position: relative;
  top: -4rpx;
}
.logo_mid {
  width: 85rpx;
  height: 65rpx;
  margin-top: 17rpx;
}
.isCouter {
  color: #2873ff;
}
 
6.在需要用到的页面文件中使用,如
//index.js
onShow(){
app.pages = getCurrentPages();
console.log(‘index.length:‘ + app.pages.length)
},
 
//index.json
{
"usingComponents": {
"tabBar": "../../component/tabBar/tabBar"
}
}
 
 
 
 
 
//index.wxml
 
<tabBar></tabBar>