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

在一个微信小程序中想要用到两种不同的tabbar样式,需要在app.js中自定义,在页面加载时进行调用。
在这里插入图片描述
第一个tabbar
首先有一个模板文件:tabbar.wxml

<template name="tabBar">    
  <view class="tab-bar" style="color: {{tabBar.color}}; background: {{tabBar.backgroundColor}}; {{tabBar.position=='top'? 'top: 0' : 'bottom: 0'}};">    
  <block wx:for="{{tabBar.list}}" wx:key="pagePath">    
    <navigator hover-class="none" 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="img"></image>    
      <image src="{{item.iconPath}}" wx:if="{{!item.active}}" class="img"></image>  
      <text class="tabbar_text">{{item.text}}</text>    
    </navigator>    
    </block>  
    <view class="clear"></view>    
  </view>    
</template> 

在app.js中自定义如下
这里我的cbl是封装好的请求

const cbl = require('utils/cbl.js');
var usercode=""
App({
  onLaunch: function () {

    var that=this;
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        if(res.data){
          usercode = res.data.UserCode;
        }else{
          usercode = ""
        }
      }
    })
  },
  //第一种底部  
  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": "#dd5923",
      "backgroundColor": "#ffffff",
      "borderStyle": "#ccc",
      "list": [
        {
          "pagePath": "/pages/kuaixundetail/kuaixundetail",
          "text": "闪阅",
          "iconPath": "http://adm.chuangbole.cn/images/cbl_bot_shandian.png",
          "selectedIconPath": "http://adm.chuangbole.cn/images/cbl_bot_shandiansel.png",
          "clas": "menu-item",
          "selectedColor": "#dd5923",
          active: true
        },
        {
          "pagePath": "/pages/index/index",
          "text": "创孵投",
          "iconPath": "http://adm.chuangbole.cn/images/cbl_bot_index.png",
          "selectedIconPath": "http://adm.chuangbole.cn/images/cbl_bot_indexsel.png",
          "selectedColor": "#dd5923",
          "clas": "menu-item",
          active: false
        },
        {
          "pagePath": "/pages/message/message",
          "text": "消息",
          "iconPath": "http://adm.chuangbole.cn/images/cbl_bot_message.png",
          "selectedIconPath": "http://adm.chuangbole.cn/images/cbl_bot_messagesel.png",
          "selectedColor": "#dd5923",
          "clas": "menu-item",
          active: false
        },
        {
          "pagePath": "/pages/mine/mine",
          "text": "我的",
          "iconPath": "http://adm.chuangbole.cn/images/cbl_bot_mine.png",
          "selectedIconPath": "http://adm.chuangbole.cn/images/cbl_bot_minesel.png",
          "selectedColor": "#dd5923",
          "clas": "menu-item",
          active: false
        }
      ],
      "position": "bottom"
    },
    //第二种底部导航栏显示
    tabBar1: {
      // "color": "#fff",
      "color": "#000",
      "selectedColor": "#dd5923",
      // "backgroundColor": "rgb(41, 38, 38)",
      "backgroundColor": "#fff",
      "borderStyle": "#fff",
      'currentTab':'0',
      "list": [
        {
          'currentTab': '0',
          "text": "发现",
          "clas": "menu-item2",
          "selectedColor": "#dd5923",
          active: false
        },
        {
          'currentTab': '1',
          "text": "创新",
          "selectedColor": "#dd5923",
          "clas": "menu-item2",
          active: true
        },
        {
          'currentTab': '2',
          "text": "",
          "iconPath": "http://adm.chuangbole.cn/images/cbl_bot_add.png",
          "selectedColor": "#dd5923",
          "clas": "menu-item2",
          active: true
        },
        {
          'currentTab': '3',
          "text": "创见",
          "selectedColor": "#dd5923",
          "clas": "menu-item2",
          active: false
        },
        {
          'currentTab': '4',
          "text": "创闻",
          "selectedColor": "#dd5923",
          "clas": "menu-item2",
          active: false
        }
      ],
      "position": "bottom"
    },
    promise: null,
  }
})

在app.wxss中定义显示样式

/**app.wxss**/
@import "/font/iconfont.wxss";
@import "/font/weui.wxss";
.menu-item{  
  width: 25%;  
  float: left;  
  text-align: center;  
  z-index: 1000;
  padding-top: 10rpx;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
}  
.menu-item2{  
  width: 20%;  
  height: 100%;
  box-sizing: border-box;
  text-align: center;  
  display: flex;
  flex-direction: column;
  justify-content:center;
  align-items: center;
}  
.img{  
  width: 27px;  
  height: 27px;  
  display: block;  
}  
.clear{  
  clear: both;  
}  
.tab-bar{  
  position: fixed;  
  width: 100%;  
  z-index: 99999;
}  
.tabbar_text{
  font-size: 22rpx;
}
.menu-item2 .tabbar_text{
  font-size: 32rpx;
  font-weight: bold;
  padding-bottom: 6rpx;
}
.menu-item2 .img{
  width:102rpx;
  height: 23px;
  /* margin-bottom: 10rpx; */
}
.menu2_border{
  font-size: 38rpx;
  font-weight: bold;
  color: #dd5923;
  border-top: 6rpx solid #dd5923;
}

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

<import src="../../template/tabbar.wxml" />
<template is="tabBar" data="{{tabBar}}" />

然后在引入模板文件页面的js中调用

const app= getApp()
Page({
  data: {
 
  },
  onShow:function(){
    app.editTabBar();    //显示自定义的底部导航
  },
  onLoad: function () {
  
  }
}) 

在这里插入图片描述
第二种tabbar
tabbar模板文件

<view class="tab-bar" style="color: {{tabBar.color}}; background: {{tabBar.backgroundColor}};display:flex;justify-content:space-between;align-items:center; {{tabBar.position=='top'? 'top: 0' : 'bottom: 0'}};">
  <block wx:for="{{tabBar.list}}" wx:key="pagePath">
    <view open-type="redirect" class="{{item.clas}} " bindtap="tabbarbind" data-current="{{item.currentTab}}">
      <image src="{{item.iconPath}}" wx:if="{{item.iconPath}}" class="img"></image>
      <text class="tabbar_text {{currentTab==item.currentTab?'menu2_border':''}}" wx:if="{{item.text}}">{{item.text}}</text>
    </view>
  </block>
  <view class="clear"></view>
</view>

js

const app= getApp()
Page({
  data: {
 
  },
  onShow:function(){
    app.editTabBar1();    //显示自定义的底部导航
  },
  onLoad: function () {
  
  }
}) 
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值