MPVUE微信小程序自定義navBar和tabBar并適配

tabBar

<template>
  <div class="tabbar-wrap"
       :style="{paddingBottom:tabBarHeight}">
    <!-- 適配蘋果手機(jī),底部安全距離 -->
    <ul>
      <li class="tabbar-item"
          v-for="(item, index) in navList"
          :key="index"
          @click="selectNavItem(index, item.pagePath)"
          :class="item.isSpecial ? 'wrapSpecial':''">
        <!-- 購物車小紅點 -->
        <!-- 未選中樣式 -->
        <p class="tabbar-icon"
           :class="(index==1&&isShowDots>0)? 'showDot-box':''">
          <span class="showDot"
                v-if="index==1&&isShowDots>0">{{isShowDots>99? '99+' : isShowDots}}</span>
          <img alt="tabbar-icon"
               mode="aspectFill"
               :src="selectNavIndex == index? item.selectedIconPath : item.iconPath"
               :class="item.isSpecial ? 'imgSpecial':''">
        </p>
        <!-- 選中樣式-->
        <p class="tabbar-text active-tabbar"
           :style="{color:selectedColor, fontWeight: 550}"
           v-if="selectNavIndex == index">{{item.text}}</p>
        <p class="tabbar-text"
           :style="{color:colors}"
           v-else>{{item.text}}</p>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  props: ['selectNavIndex', 'isShowDots'],
  data () {
    return {
      tabBarHeight: 0,
      colors: "#666",
      selectedColor: "#000",
      navList: []
    }
  },
  watch: {
    isShowDots () {//動態(tài)更新購物車數(shù)量
      this.isShowDots = this.isShowDots
    }
  },

  created () {
    let that = this
    that.tabBarHeight = 0
    that.colors = that.globalData.tabbarColor//全局tabBar未選中字體顏色
    that.selectedColor = that.globalData.tabbarActiveColor//全局tabBar選中時字體顏色
    this.buId = parseInt(this.globalData.buId)//buid
    /* 
      tabBar的選中和未選中的圖標(biāo)和字體顏色都是根據(jù)buId動態(tài)配置的
    */
    this.navList = [
      {
        pagePath: "/pages/home/main",
        iconPath: "/static/images/home-icon.png",
        selectedIconPath: "/static/module" + that.buId + "/tabBar-home-active.png",
        text: "首頁"
      },
      {
        pagePath: "/pages/cart/main",
        iconPath: "/static/images/cat-icon.png",
        selectedIconPath: "/static/module" + that.buId + "/tabBar-cat-active.png",
        text: "購物車"
      },
      {
        pagePath: "/pages/personal/main",
        iconPath: "/static/images/person-icon.png",
        selectedIconPath: "/static/module" + that.buId + "/tabBar-person-active.png",
        text: "我的"
      }
    ],

      wx.getSystemInfo({
        success: res => {
          // 獲取手機(jī)的底部安全距離
          that.tabBarHeight = 0
          that.tabBarHeight = res.screenHeight - res.safeArea.bottom + 'px';
        }
      })
  },
  methods: {
    /**
     * 點擊導(dǎo)航欄
     */
    selectNavItem (index, pagePath) {
      if (index === this.selectNavIndex) {
        return false;
      }
      this.bindNavigateTo(pagePath);
    },

    /**
     * 路由跳轉(zhuǎn)
     */
    bindNavigateTo (url) {
      wx.switchTab({
        url
      })
    },
  }

}
</script>

導(dǎo)航欄

<template>
  <div class="comp-navbar">
    <!-- 導(dǎo)航欄主體 -->
    <cover-view class="navbar"
                :style="{height: navBarHeight + 'px'}">
      <!-- 狀態(tài)欄 -->
      <cover-view class="nav-statusbar"
                  :style="{height: statusBarHeight + 'px'}"></cover-view>
      <!-- 標(biāo)題欄 -->
      <cover-view class="nav-titlebar"
                  :style="{height: titleBarHeight + 'px' }">
        <!-- home及后退鍵 -->
        <cover-view class="bar-options">
          <cover-image class="nav-icon"
                       mode="aspectFill"
                       @click="backClick"
                       src="/static/images/arrow-left.png"></cover-image>
        </cover-view>
        <!-- 導(dǎo)購信息 -->
        <cover-view class="guide-box"
                    v-if="guideInfo.name">
          <cover-image mode="aspectFill"
                       class="guide-img"
                       style="width: 29px;height: 29px;"
                       :src="guideInfo.idPic?guideInfo.idPic:guideInfo.pic"></cover-image>
          <cover-view class="guide-p">來自{{guideInfo.name}}的推薦</cover-view>
        </cover-view>
        
      </cover-view>
    </cover-view>
  </div>
</template>


 //適配蘋果手機(jī)安全距離
  beforeMount () {
    const self = this;
    wx.getSystemInfo({
      success (system) {
        self.statusBarHeight = system.statusBarHeight;
        self.titleBarHeight = 48;
        self.navBarHeight = self.statusBarHeight + self.titleBarHeight;
      }
    });
  },

    backClick () {
    // 如果是分享進(jìn)來的沒有歷史路由就返回首頁,否則就返回上一頁
      if (getCurrentPages().length == 1) {
        wx.switchTab({
          url: this.homePath
        })
      } else {
        wx.navigateBack({
          delta: 1
        })
      }
    },

?著作權(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)容

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