小程序开发-自定义导航栏组件

上一篇文章讲到了小程序设置全屏,隐藏导航栏的方式。

主要是配置自定义导航栏"navigationStyle": "custom"

这篇文章尝试自定义导航栏

1. 首先,我们要获取状态栏的高度,以及胶囊的位置

可以通过getSystemInfo 方法

 //获取菜单按钮(右上角胶囊按钮)的布局位置信息
  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.statusBarHeight = statusBarHeight ;//状态栏高度
      this.globalData.navHeight = navHeight;//导航栏高度
      this.globalData.navTop = navTop;//胶囊按钮与顶部的距离
      this.globalData.jnHeight = menuButtonObject.height;//胶囊的高度
      this.globalData.jnWidth = menuButtonObject.width;//胶囊的宽度
    },
    fail(err) {
      console.log(err);
    }

2. 自定义导航栏 navigationBar

  • navigationBar.js 代码
const app = getApp()

Component({

  properties: {
    title: {
      type: String,
      value: 'weiChat'
    },
    back: {
      type: Boolean,
      value: false
    },
    home: {
      type: Boolean,
      value: false
    }
  },
  data: {
    navigationBarHeight: app.globalData.navHeight 
    statusBarHeight: app.globalData.statusBarHeight
  },

  methods: {
    backHome: function () {
      let pages = getCurrentPages()
      wx.navigateBack({
        delta: pages.length
      })
    },
    back: function () {
      wx.navigateBack({
        delta: 1
      })
    },
    loading: {
      type: Boolean,
      value: false
    }
  }
})
  • navigationBar.wxss 代码
    白色导航栏,
.navbar {
   100%;
  background-color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
}
.title-container {
  height: 40px;
  display: flex;
  align-items: center;
  position: relative;
}
.capsule {
  margin-left: 10px;
  height: 30px;
  background: rgba(255, 255, 255, 0.6);
  border: 1px solid #eee;
  border-radius: 16px;
  display: flex;
  align-items: center;
}
.capsule > view {
   45px;
  height: 60%;
  position: relative;
}
.capsule > view:nth-child(2) {
  border-left: 1px solid #eee;  
}
.capsule image {
   20px;
  height: 20px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-10px,-10px);
}
.title {
  position: absolute;
  top: 6px;
  left: 104px;
  right: 104px;
  height: 30px;
  line-height: 30px;
  font-size: 18px;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.navBar-loader {
	height: 18px;
	 18px;
  display: inline-block;
  margin-right: 4px;
  vertical-align: middle;
}

  • navigationBar.wxml 代码
<view class="navbar" style="{{'height: ' + navigationBarHeight}}">
  <view style="{{'height: ' + statusBarHeight}}"></view>
  <view class='title-container'>
    <view class='capsule' wx:if="{{ back || home }}">
      <view bindtap='back' wx:if="{{back}}">
        <image src='/images/back.png'></image>         
      </view>
      <view bindtap='backHome' wx:if="{{home}}">
        <image src='/images/home_top.png'></image>
      </view>
    </view>
    <view class='title'><view wx:if="{{loading}}" class="navBar-loader"><view class="inner"></view></view>{{text}}</view>
  </view>
</view>
<view style="{{'height: ' + navigationBarHeight}};background: white;"></view>

3. 使用

<navigationBar title="{{title}}" loading="{{true}}" home="{{true}}" back="{{true}}"></navigationBar>
原文地址:https://www.cnblogs.com/limaostudio/p/13626840.html