微信小程序自定义顶部以及底部tabbar

1.首先建立一个模板,在里边模板的wxml里绘制公共的顶部以及底部
代码如下:





2.然后在模板的wxss里样式

 /* pages/template/template.wxss */
.tabbar_box{
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: 999;
    width: 100%;
    height: 120rpx;
    border-top: 1rpx solid #d7d7d7; 
}

.tabbar_nav{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 28rpx;
    height: 100%;
}

.tabbar_icon{
    width: 50rpx;
    height:50rpx;
}

.tabbar_nav:nth-child(2) image{ 
    position:relative;
    top:-32rpx;
    width:80rpx;
    height:80rpx;
}
.tabbar_nav:nth-child(2) text{  
    position:relative; 
    top:-32rpx;
}
 /* 顶部 */
.ico-back{
  width: 36rpx;
  height: 36rpx;
  background-size: contain;
  background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAYAAADhAJiYAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMDY3IDc5LjE1Nzc0NywgMjAxNS8wMy8zMC0yMzo0MDo0MiAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIDIwMTUgKFdpbmRvd3MpIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOjAwRjFBMThDOEU1NDExRThCQUMxRTFBRUNDRDNCMkJEIiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOjAwRjFBMThEOEU1NDExRThCQUMxRTFBRUNDRDNCMkJEIj4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6MDBGMUExOEE4RTU0MTFFOEJBQzFFMUFFQ0NEM0IyQkQiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6MDBGMUExOEI4RTU0MTFFOEJBQzFFMUFFQ0NEM0IyQkQiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz7a0EjNAAAA80lEQVR42sTYOw7DIAwGYNxuPXHnTtygvUAuWalyE6WRMlBj7N/GEkOiCD4B4eHCzEUq3ljruPMe9fcstxcJOmGOqNNADcwR+SABs6SDOphrKqiHSZ3UGkwaSItJAY1gwkGjmFCQBRMGsmJCQB4MHOTFQEEIDAyEwkBASIwGRIrFr/XBZy03InpbQFJcFHW8Gu+2nnmUiFAM2daLzz/DVtHtaSc1DIX87SEo9MLoRkVsHS5U1OZqRkUeP0yo6APaMCrjCDuEyjrkq1GZ1yAVKvui2EXNuEqLqFnJBgk1LR3TQi08OWF1RqmyH8SAtB0yvgIMAGA41d9Fo4AZAAAAAElFTkSuQmCC);
  background-repeat: no-repeat;
  background-position-x: 20rpx;
}
.hh-header {
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 99;
}
.hh-title{
  font-size: 0.8rem;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}
.title-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.hh-nav-right{
  width: 116px;
}
.hh-nav-back{
  width: 116px;

}

3.在app.js里定义参数以及方法

//app.js
App({

  globalData: {
    themeArr: ['gray', 'black', 'green', 'orange', 'pink', 'blue'],
    theme: 'black', // gray, black, green, orange, pink, blue
    statusBarHeight:0,
    titleBarHeight:0,
    color:'white',
  },
  tabbar: {
    color: "#707070",
    selectedColor: "#1e90ff",
    backgroundColor: "#F2F2F2",
    borderStyle: "#d7d7d7",
    list: [
      {
        pagePath: "/pages/enterprise/home/home/home",
        text: "首页",
        iconPath: "/images/home_normal.png",
        selectedIconPath: "/images/home_blue_selected.png"
      },
      {
        pagePath: "/pages/enterprise/product/product/product",
        text: "商品",
        iconPath: "/images/product_normal.png",
        selectedIconPath: "/images/product_blue_selected.png"
      },
      {
        pagePath: "/pages/enterprise/info/info/info",
        text: "企业信息",
        iconPath: "/images/info_normal.png",
        selectedIconPath: "/images/info_blue_selected.png"
      },
      {
        pagePath: "/pages/enterprise/shopcar/shopcar/shopcar",
        text: "购物车",
        iconPath: "/images/cart_normal.png",
        selectedIconPath: "/images/cart_blue_selected.png"
      },
      {
        pagePath: "/pages/enterprise/mine/mine/mine",//跳转链接
        text: "我的",//文字
        iconPath: "/images/contact_normal.png",//点击前
        selectedIconPath: "/images/contact_blue_selected.png"//点击后
      }
    ],
    position: "bottom"
  },
  /*修改底部图片以及字体颜色*/
  upTabList: function () {
    var that = this;
    var _curPageArr = getCurrentPages();
    var _curPage = _curPageArr[_curPageArr.length - 1];
    var tabbar = this.tabbar
    var globalData = this.globalData
    switch (globalData.theme) {
      case 'black':
        tabbar.selectedColor="#fff"
        tabbar.list[0].iconPath = "/images/home_normal.png",
          tabbar.list[0].selectedIconPath = "/images/home_black_w.png",
          tabbar.list[1].iconPath = "/images/product_normal.png",
          tabbar.list[1].selectedIconPath = "/images/product_black_selected.png"
        tabbar.list[2].iconPath = "/images/info_normal.png",
          tabbar.list[2].selectedIconPath = "/images/info_black_selected.png"
        tabbar.list[3].iconPath = "/images/cart_normal.png",
          tabbar.list[3].selectedIconPath = "/images/cart_black_selected.png"
        tabbar.list[4].iconPath = "/images/contact_normal.png",
          tabbar.list[4].selectedIconPath = "/images/contact_black_selected.png"
        break;
    }
    _curPage.setData({
      tabbar: tabbar,
    });
  },
  /*修改颜色*/
  upBackground: function (backgroundColor) {
    var tabBar = this.tabbar
    var globalData = this.globalData
    var _curPageArr = getCurrentPages();
    var _curPage = _curPageArr[_curPageArr.length - 1];
    tabBar.backgroundColor = backgroundColor
    globalData.theme = backgroundColor
    _curPage.setData({
      tabbar: tabBar,
      globalData: globalData
    });
  },
  changeTabBar: function () {
    var _curPageArr = getCurrentPages();
    var _curPage = _curPageArr[_curPageArr.length - 1];
    var _pagePath = _curPage.__route__;
    if (_pagePath.indexOf('/') != 0) {
      _pagePath = '/' + _pagePath;
    }
    var tabBar = this.tabbar;
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].selected = false;
      if (tabBar.list[i].pagePath == _pagePath) {
        tabBar.list[i].selected = true;//根据页面地址设置当前页面状态  
      }
    }
    _curPage.setData({
      tabbar: tabBar
    });
  },
  /*适配顶部高度*/
  onLaunch: function () {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.globalData.platform = res.platform
        let totalTopHeight = 68
        if (res.model.indexOf('iPhone X') !== -1) {
          totalTopHeight = 88
        } else if (res.model.indexOf('iPhone') !== -1) {
          totalTopHeight = 64
        }
        that.globalData.statusBarHeight = res.statusBarHeight
        that.globalData.titleBarHeight = totalTopHeight - res.statusBarHeight
        console.log(that.globalData.titleBarHeight)
      },
      failure() {
        that.globalData.statusBarHeight = 0
        that.globalData.titleBarHeight = 0
      }
    })
  }
})

4.在引入模板页面的js里接收数据

// pages/enterprise/home/home/home.js
var sTab = require('../../../../utils/switchTab.js');
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar: {},
    theme: app.globalData.theme,
    topcontent: {
      name: '首页',
      background: app.globalData.theme,
      color: app.globalData.color,
      statusBarHeight: app.globalData.statusBarHeight,
      titleBarHeight: app.globalData.titleBarHeight,
      isShowBack: false,
    },
  },

  //切换选项
  switchTab: function (e) {
    sTab.switchTab(e);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    app.changeTabBar(); 
    app.upTabList();
    app.upBackground(app.globalData.theme);
  },




})


新建一个公用js
//点击切换tabbar
function switchTab(e) {
  var url = e.currentTarget.dataset.url;
  wx.redirectTo({
    url: url
  });
}
//返回上一页
function goback() {
  wx.navigateBack({
    delta: 1,
  })
}
module.exports = {
  switchTab: switchTab,
  goback:goback,
}

5.最后一步在页面引入模板,然后就可以使用啦