微信小程序自定義導航欄組件


在微信小程序開發過程中,經常會有特殊需求自定義導航欄,本篇博客介紹如何自定義導航欄組件,可在多頁面使用

1、在 onLaunch 方法中獲取系統導航欄布局信息存入全局變量

App({
  //設置導航欄
  //獲取菜單按鈕的布局位置信息
  let menuButtonObject = wx.getMenuButtonBoundingClientRect();
  //獲取系統信息
  wx.getSystemInfo({
    success: res => {
      //狀態欄的高度
      let statusBarHeight = res.statusBarHeight,
      //膠囊按鈕與頂部的距離
      navTop = menuButtonObject.top, 
      navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2; 
      this.globalData.navHeight = navHeight;//導航欄高度
      this.globalData.navTop = navTop;//膠囊按鈕與頂部的距離
      this.globalData.jnheight = menuButtonObject.height;//膠囊的高度
      this.globalData.jnwidth = menuButtonObject.width;//膠囊的寬度
    },
    fail(err) {
      console.log(err);
    }
  });
  //設置全局對象
  globalData: {
    navHeight: 0, 
    navTop: 0,     
    jnheight: 0,
    jnwidth: 0,   
  }
})

2、自定義 navbar 組件

2.1、navbar.wxml代碼

<view class="navbar" style="height:{{navHeight+5}}px;">
  <!-- 左上角 返回按鈕 和 home按鈕 wx:if="{{showNav}}" 是控制左上角按鈕的顯示隱藏,首頁不顯示 -->
  <view class="navbar_left" 
  style="top:{{navTop}}px;height:{{jnheight-1}}px;width:{{jnwidth-3}}px" 
  wx:if="{{showNav}}">
    <!-- 控制返回按鈕的顯示 -->
    <view bindtap="navBack">
      <image src="../../images/back.png" mode="widthFix" style="width:40%"></image>
    </view>
    <!-- home按鈕 wx:if="{{showHome}}" 是控制左上角 home按鈕的顯示隱藏-->
    <view class="nav_line" bindtap="navHome" wx:if="{{showHome}}">
      <image src="../../images/backhome.png" mode="widthFix" style="width:50%"></image>
    </view>
  </view>
  <!-- 中間標題 -->
  <view class="navbar_title" style="top:{{navTop}}px;">{{pageName}}</view>
</view>

2.2、navbar.js代碼

const App = getApp();
Component({
  // 組件的屬性列表
  properties: {
    pageName: String, //中間的title
    showNav: { //判斷是否顯示左上角的按鈕    
      type: Boolean,
      value: true
    },
    showHome: { //判斷是否顯示左上角的home按鈕
      type: Boolean,
      value: true
    }
  },
  // 組件的初始數據
  data: {
    showNav: true, //判斷是否顯示左上角的home按鈕
    showHome: true, //判斷是否顯示左上角的按鈕
  },
  lifetimes: {
    // 生命周期函數,可以為函數,或一個在methods段中定義的方法名
    attached: function() {
      this.setData({
        navHeight: App.globalData.navHeight, //導航欄高度
        navTop: App.globalData.navTop, //膠囊按鈕與頂部的距離
        jnheight: App.globalData.jnheight, //膠囊高度
        jnwidth: App.globalData.jnwidth //膠囊寬度
      })
    }
  },
  // 組件的方法列表
  methods: {
    //回退
    navBack: function() {
      wx.navigateBack()
    },
    //回主頁
    navHome: function() {
      wx.switchTab({
        url: '/pages/home/home'
      })
    },
  }
})

2.3、navbar.wxss代碼

.navbar {
  width: 100%;
  overflow: hidden;
  position: sticky;
  top: 0;
  left: 0;
  z-index: 10;
  flex-shrink: 0;
  background: #fff;
}

.navbar_left {
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
  position: absolute;
  left: 10px;
  z-index: 11;
  line-height: 1;
  border: 1rpx solid #f0f0f0;
  border-radius: 40rpx;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.6);
}

.navbar_left view {
  width: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.nav_line {
  border-left: 1rpx solid #f0f0f0;
}

.navbar_title {
  width: 100%;
  box-sizing: border-box;
  padding-left: 115px;
  padding-right: 115px;
  height: 32px;
  line-height: 32px;
  text-align: center;
  position: absolute;
  left: 0;
  z-index: 10;
  color: #333;
  font-size: 16px;
  font-weight: bold;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

3、使用 navbar 組件

.json文件中設置自定義導航欄,並引入 navbar 組件

{
  "navigationStyle":"custom",
  "usingComponents":{
    "navbar":"/components/navbar/navbar"
  }
}

.wxml文件中使用 navbar 組件

<navbar page-name="{{shoppingName}}" show-nav="{{showNav}}" show-home="{{showHome}}"></navbar>

End!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM