微信小程序之如何自定義底部tabbar導(dǎo)航

我之前寫一個微信小程序血久,保單萬事通氧吐,有一個這樣的功能


image.png

這個底部導(dǎo)航末盔,如果用小程序自帶的tabbar,根本無法實(shí)現(xiàn)翠拣,所有我想第2種方法來實(shí)現(xiàn)
微信小程序文檔中游盲,有一種這個方法,但是我不建議谜慌,體驗(yàn)極差畦娄。弊仪。。驳癌。使用首先可以通過在app.json里放入


{
  "usingComponents": {
    "mp-tabbar": "../../components/tabbar/tabbar"
  }
}

然后組件引入

<mp-tabbar style="position:fixed;bottom:0;width:100%;left:0;right:0;" list="{{list}}" bindchange="tabChange"></mp-tabbar>

// page.js示例代碼

Page({
    data: {
        list: [{
            text: "對話",
            iconPath: "/example/images/tabbar_icon_chat_default.png",
            selectedIconPath: "/example/images/tabbar_icon_chat_active.png",
        },
        {
            text: "設(shè)置",
            iconPath: "/example/images/tabbar_icon_setting_default.png",
            selectedIconPath: "/example/images/tabbar_icon_setting_active.png",
            badge: 'New'
        }]
    },
    tabChange(e) {
        console.log('tab change', e)
    }
});

但是這樣寫有個問題颓鲜,就是換頁面的時候,會導(dǎo)致整個頁面重新刷新甜滨,用戶體驗(yàn)不好
所以我建議,通過在app.json里放入tabbar

js
 "tabBar": {
    "custom": true,//組件替換屬性
    "color": "#7A7E83",
    "selectedColor": "#333333",
    "borderStyle": "black",
    "backgroundColor": "#ffffff",
    "list": [
      {
        "pagePath": "pages/guarantee/guarantee",
        "iconPath": "assets/img/1.png",
        "selectedIconPath": "assets/img/2.png",
        "text": "保單萬事通"
      },
      {
        "pagePath": "pages/add/add",
        "iconPath": "assets/img/add.png",
        "text": "添加保單"
      },
      {
        "pagePath": "pages/my/my",
        "iconPath": "assets/img/6.png",
        "selectedIconPath": "assets/img/7.png",
        "text": "我的"
      }
    ]
  },

注意我加了一個屬性 "custom": true衣摩,這個如果不加,就只能使用微信自帶的tabbar底部導(dǎo)航了既琴,無法按照自己的想法設(shè)計(jì)泡嘴,所以,當(dāng)你加了custom這個屬性后磺箕,你只需要,在根目錄下滞磺,添加一個組件 cumtom-tab-bar,你甚至都不需要去引進(jìn)击困,小程序會自己去找這個組件广凸,切記,放到根目錄下谅海,然后,你就可以隨心所欲控制底部tabbar了



Component({
  data: {
    selected: 0,
    color: "#7A7E83",
    selectedColor: "#333333",
    canCustom: true,
    list: [
      {
        "pagePath": "/pages/guarantee/guarantee",
        "iconPath": "/assets/img/1.png",
        "selectedIconPath": "/assets/img/2.png",
        "text": "保單"
      },
      {
        "pagePath": "/pages/add/add", 
        "iconPath": "/assets/img/4.png",
        "selectedIconPath": "/assets/img/add.png",
        "text": "添加保單"
      },
      {
        "pagePath": "/pages/my/my",
        "iconPath": "/assets/img/6.png",
        "selectedIconPath": "/assets/img/7.png",
        "text": "我的"
      }
    ]
  },
  created(){
   
  },
  lifetimes: {
    attached: function () {
      
      
      // getApp().watch(this.watchBack,this)
     
    },
  },
  attached: function () {
    // getApp().watch(this.watchBack, this)

  },


  methods: {

    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path;
      console.log(url);
      var pages = getCurrentPages() //獲取加載的頁面

      // var currentPage = pages[pages.length - 1] //獲取當(dāng)前頁面的對象

      // var urls = currentPage.route //當(dāng)前頁面url

     // var options = currentPage.options //如果要獲取url中所帶的參數(shù)可以查看options
     // console.log("route = ", urls);
      console.log(url)
      if (url == "/pages/add/add"){
        getApp().globalData.switctTime++;
        if (getApp().globalData.switctTime>1){
          getApp().globalData.switctTime = 0;
      
          wx.switchTab({
            url: getApp().globalData.userUrl
          })
          return false;
        }else{
          wx.switchTab({
            url:"/pages/add/add"
          })
          return false;
        }
        
        // if (urls == "pages/add/add"){
        //   getApp().globalData.userUrl = 1
        // }
      }else{
        getApp().globalData.switctTime = 0;
        getApp().globalData.userUrl = url;
      }
      // if(url==undefined){
      //   this.setData({
      //     selected: data.index
      //   })    
      //   return;
      // }
      wx.switchTab({
        url:url
      })
      this.setData({
        selected: data.index
      })
    }
  }
})
wxml
<!--miniprogram/custom-tab-bar/index.wxml-->
<cover-view id='customTabBar' class="tab-bar" wx:if="{{canCustom}}">

  <cover-view class="tab-bar-border"></cover-view>
  <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <cover-image src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
    <cover-view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>

  </cover-view>



</cover-view>
wxss
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 48px;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 10000;
}

.tab-bar-border {
  background-color: rgba(0, 0, 0, 0.33);
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(0.5);
}

.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  position: relative;
}

.tab-bar-item cover-image {
  width: 27px;
  height: 27px;
}

.tab-bar-item cover-view {
  font-size: 10px;
}


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蝌诡,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子浦旱,更是在濱河造成了極大的恐慌九杂,老刑警劉巖宣蠕,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件抢蚀,死亡現(xiàn)場離奇詭異涎永,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進(jìn)店門妈倔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绸贡,“玉大人,你說我怎么就攤上這事听怕。” “怎么了闽烙?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵黑竞,是天一觀的道長疏旨。 經(jīng)常有香客問我,道長檐涝,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任幅聘,我火速辦了婚禮喊暖,結(jié)果婚禮上撕瞧,老公的妹妹穿的比我還像新娘狞尔。我一直安慰自己巩掺,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布研儒。 她就那樣靜靜地躺著独令,像睡著了一般。 火紅的嫁衣襯著肌膚如雪冲呢。 梳的紋絲不亂的頭發(fā)上招狸,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天,我揣著相機(jī)與錄音乘凸,去河邊找鬼。 笑死营勤,一個胖子當(dāng)著我的面吹牛信柿,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播渔嚷,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼形病,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了量瓜?” 一聲冷哼從身側(cè)響起途乃,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎猎塞,沒想到半個月后杠纵,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡铝量,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年慢叨,在試婚紗的時候發(fā)現(xiàn)自己被綠了群凶。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖力穗,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情够坐,我是刑警寧澤元咙,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布庶香,位于F島的核電站,受9級特大地震影響赶掖,放射性物質(zhì)發(fā)生泄漏七扰。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一膳灶、第九天 我趴在偏房一處隱蔽的房頂上張望立由。 院中可真熱鬧序厉,春花似錦聋迎、人聲如沸脂矫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽庭再。三九已至,卻和暖如春牺堰,著一層夾襖步出監(jiān)牢的瞬間拄轻,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工伟葫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留恨搓,地道東北人。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓筏养,卻偏偏與公主長得像斧抱,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子渐溶,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容