微信小程序自定义导航栏(含代码&效果图)

做成组件,方便多页面引用

app.js的onLaunch里取得状态栏胶囊的信息并存到全局变量globalData

app.js
  onLaunch: function(option) {
    var that = this
    //设置导航栏信息
    this.getNavBarInfo()

  },
  getNavBarInfo() {
    // 获取系统信息
    const systemInfo = wx.getSystemInfoSync();
    // 胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    // 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度
    this.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight;
    this.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight;
    this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
    this.globalData.menuHeight = menuButtonInfo.height;
    this.globalData.menuWidth= menuButtonInfo.width;
  },
  // 全局变量
  globalData: {
    // 导航栏高度
    navBarHeight: 0, 
    // 胶囊距底部间距(保持底部间距一致)
    menuBotton: 0, 
    // 胶囊距右方间距(方保持左、右间距一致)
    menuRight: 0,
    // 胶囊高度(自定义内容可与胶囊高度保证一致)
    menuHeight: 0,
    //胶囊宽度(自定义内容可与胶囊宽度保证一致)
    menuWidth:0
  },

navigation.wxml

<view class="nav" style="height:{
  {navBarHeight}}px;">
  <view class="nav-main">
    <view 
      class="capsule-box" 
      style="height:{
  {menuHeight}}px; min-height:{
  {menuHeight}}px; line-height:{
  {menuHeight}}px; bottom:{
  {menuBotton}}px;left:{
  {menuRight}}px;">
    <!--
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值