微信小程序自定義導(dǎo)航欄

微信小程序官方文檔提供的導(dǎo)航是在app.json文件內(nèi)定義的,

這里所定義的導(dǎo)航是全局導(dǎo)航欄

但是在實(shí)際的項(xiàng)目,更多時(shí)候是不需要全局的導(dǎo)航欄,啊就只有自己定義導(dǎo)航欄了

創(chuàng)建一個(gè)template文件夾,存放公共的模板,我們自定義的導(dǎo)航欄就是一個(gè)導(dǎo)航欄
nav.wxml

<template name="nav">
  <view class="nav_link" bindtap="{{fn}}">
    <view class="defalut_btn {{current== 0 ? '' : 'on_cor'}}">
      <block wx:if="{{current == 0}}">
        <image src="{{ico}}" class="iconfont  del_ico idx_ico"></image>
        <view class="txt">{{name}}</view>
      </block>
      <block wx:if="{{current == 1}}">
        <image src="{{selecIcon}}" class="iconfont  del_ico idx_ico"></image>
        <view class="txt txt_fb">{{name}}</view>
      </block>
    </view>
  </view>
</template>

樣式app.wxss內(nèi)設(shè)置即可。

在需要展示的導(dǎo)航欄的頁(yè)面中直接引入nav.wxml即可,

<!-- 底部導(dǎo)航 -->
<import src="../../template/nav" />
<view class="flex fix_nav_wp">
  <block wx:for="{{navData}}" wx:key="">
    <template is="nav" data="{{...item}}" />
  </block>
</view>

navData是自定義導(dǎo)航欄的數(shù)據(jù)。直接在data里面設(shè)置

data:{
    navData: [{
      name: "首頁(yè)", //文本
      current: 1, //是否是當(dāng)前頁(yè),0不是  1是
      style: 0, //樣式
      ico: '../../images/hone.png', //不同圖標(biāo)
      fn: 'gohome', //對(duì)應(yīng)處理函數(shù)
      selecIcon: "../../images/select-home.png"
    }, {
      name: "消息",
      current: 0,
      style: 0,
      ico: '../../images/information.png',
      fn: 'goMes',
      selecIcon: "../../images/select-information.png"
    }, {
      name: "設(shè)置",
      current: 0,
      style: 1,
      ico: '../../images/set.png',
      fn: 'goSetting',
      selectIcon: "../../images/select-set.png"
    }],
}

//對(duì)應(yīng)函數(shù)
goMes: function() {
    if (this.data.isClicked == false) {
      util.isClick(this);
      wx.reLaunch({
        url: '/pages/message/message',
      })
    } else {
      util.forbid()
    }
  },
  goSetting: function() {
      wx.reLaunch({
        url: '/pages/setting/setting',
      })
  },

以上是我常用的自定義導(dǎo)航欄,希望能夠幫到大家!

歡迎留言

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

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

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