微信小程序動態(tài)tabBar實現(xiàn)

最近做項目的時候,突然來了個小特殊的需求,根據(jù)客戶的類型來動態(tài)顯示底部的tabBar菜單。當(dāng)時我就有點小懵逼了,這個不是小程序自帶的組件么?還要做成動態(tài)?這就有點尷尬了.....
不過也只是一時尷尬而已,然后我就展開了搜索之旅.....然后發(fā)現(xiàn),官方的組件確實沒辦法做動態(tài),那咋辦,如果真的有這個需求那也是得去處理滴呀~然后也看了有一些做到這效果的方法,那就試一下唄。。其實就是自定義個tabBar的模板,以下是實現(xiàn):

首先,既然是說自定義組件,那是用到template了。那先在Page里新建個template的文件夾,以便放tabBar的組件。

TIM圖片20171128141629.png

然后新建個tabBar.wxml文件,這里就寫下你的tabBar的結(jié)構(gòu)。


<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="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>

下面是tabBar所需要用到的樣式,我這里就直接寫在全局app.wxss了。

.menu-item{  
  width: 32%;  
  float: left;  
  text-align: center;  
  padding-top: 8px;  
}  
.menu-item2{  
  width: 24%;  
  float: left;  
  text-align: center;  
  padding-top: 8px;  
}  
.img{  
  width: 30rpx;  
  height: 30rpx;  
  display: block;  
  margin:auto;  
}  
.clear{  
  clear: both;  
}  
.tab-bar{  
  position: fixed;  
  width: 100%;  
  padding: 0px 2%;  
}  
.tabbar_text{
  font-size: 28rpx
}

然后接下來是js的部分,由于是底部的導(dǎo)航,那肯定是不止一個頁面用到的,那這里就可以寫在全局的app.js里面方便使用。這里我寫了兩種tabBar的模板,分別對應(yīng)來顯示

//app.js
App({
  onLaunch: function () {
    // // 展示本地存儲能力
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)

  },
  //第一種狀態(tài)的底部  
  editTabBar: function () {
    var _curPageArr = getCurrentPages();
    var _curPage = _curPageArr[_curPageArr.length - 1];
    var _pagePath = _curPage.__route__;
    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;//根據(jù)頁面地址設(shè)置當(dāng)前頁面狀態(tài)    
      }
    }
    _curPage.setData({
      tabBar: tabBar
    });
  },
  //第二種狀態(tài)的底部  
  editTabBar2: function () {
    var _curPageArr = getCurrentPages();
    var _curPage = _curPageArr[_curPageArr.length - 1];
    var _pagePath = _curPage.__route__;
    if (_pagePath.indexOf('/') != 0) {
      _pagePath = '/' + _pagePath;
    }
    var tabBar = this.globalData.tabBar2;
    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;//根據(jù)頁面地址設(shè)置當(dāng)前頁面狀態(tài)    
      }
    }
    _curPage.setData({
      tabBar: tabBar
    });
  },  
  globalData: {
    userInfo: null,
    pop:2,
    num:0,
    tabBar: {
      "color": "#9E9E9E",
      "selectedColor": "#f00",
      "backgroundColor": "#fff",
      "borderStyle": "#ccc",
      "list": [
        {
          "pagePath": "/pages/index/index",
          "text": "首頁",
          "iconPath": "/img/home.png",
          "selectedIconPath": "/img/home_on.png",
          "clas": "menu-item",
          "selectedColor": "#4665f9",
          active: true
        },
        {
          "pagePath": "/pages/log/index",
          "text": "日志",
          "iconPath": "/img/home.png",
          "selectedIconPath": "/img/home_on.png",
          "selectedColor": "#4665f9",
          "clas": "menu-item",
          active: false
        },
        {
          "pagePath": "/pages/my/index",
          "text": "我的",
          "iconPath": "/img/home.png",
          "selectedIconPath": "/img/home_on.png",
          "selectedColor": "#4665f9",
          "clas": "menu-item",
          active: false
        }
      ],
      "position": "bottom"
    },
    tabBar2: {
      "color": "#9E9E9E",
      "selectedColor": "#f00",
      "backgroundColor": "#fff",
      "borderStyle": "#ccc",
      "list": [
        {
          "pagePath": "/pages/index/index",
          "text": "首頁",
          "iconPath": "/img/home.png",
          "selectedIconPath": "/img/home_on.png",
          "clas": "menu-item2",
          "selectedColor": "#4665f9",
          active: true
        },
        {
          "pagePath": "/pages/log/index",
          "text": "日志",
          "iconPath": "/img/home.png",
          "selectedIconPath": "/img/home_on.png",
          "selectedColor": "#4665f9",
          "clas": "menu-item2",
          active: false
        },
        {
          "pagePath": "/pages/my/index",
          "text": "我的",
          "iconPath": "/img/home.png",
          "selectedIconPath": "/img/home_on.png",
          "selectedColor": "#4665f9",
          "clas": "menu-item2",
          active: false
        },
        {
          "pagePath": "/pages/new/index",
          "text": "新的",
          "iconPath": "/img/home.png",
          "selectedIconPath": "/img/home_on.png",
          "selectedColor": "#4665f9",
          "clas": "menu-item2",
          active: false
        }
      ],
      "position": "bottom"
    }
  }
})

然后在需要用到這個組件的頁面上直接調(diào)用。比如這里的index頁面。

<!--index.wxml-->
<import src="../template/tabbar.wxml"/>
首頁
<template is="tabBar" data="{{tabBar:tabBar}}"></template>

image.png

然后去index.js里面調(diào)用tabBar


image.png

然后下面是效果圖。


image.png

就這些。我個人覺得這個自定義導(dǎo)航的用戶體驗不是很好,能不用就不要用,不過知道下方法也是ok滴!如有發(fā)現(xiàn)有錯或者不足的地方可以指出,謝謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,057評論 25 709
  • WXML WXML(WeiXin Markup Language)是微信的一套標(biāo)簽語言,結(jié)合基礎(chǔ)組件、事件系統(tǒng),可...
    許劍鋒閱讀 7,289評論 3 51
  • 昨天看了一下微信小程序官方文檔,總結(jié)一下自己學(xué)習(xí)的個人心得. 首先從官方文檔給的框架說起,微信小程序官方文檔給出了...
    Mr大大大閱讀 47,514評論 9 68
  • 文/梓星 深夜 我看見一只蝴蝶 漸漸干癟的軀體 它終究會成為美麗的標(biāo)本 卻再也無法在春天里 為花朵兒跳一支舞 清晨...
    梓莘閱讀 309評論 7 8
  • 安裝ionic/Install Ionic 首先您需要安裝Node.js. 其次, 安裝最新版本的cordova ...
    發(fā)發(fā)呆喲閱讀 332評論 0 0

友情鏈接更多精彩內(nèi)容