微信小程序自定义导航栏

目录

实现原理

2.1获取胶囊的详细信息

2.2获取导航栏的整体高度 

2.3获取胶囊距离右边的距离 

2.4完整获取代码: 

组件代码


实现原理

2.1获取胶囊的详细信息

let menuButtonObject = wx.getMenuButtonBoundingClientRect();
  •  width:胶囊的宽度;
  • height:胶囊的高度
  • top:胶囊距离顶部的距离

2.2获取导航栏的整体高度 

 

wx.getSystemInfo({
   success: res => {
    let statusBarHeight = res.statusBarHeight,navTop = menuButtonObject.top,
    navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
   }
})

整体高度: 状态栏高度+胶囊高度+(胶囊距离-胶囊高度)*2 

    navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;

2.3获取胶囊距离右边的距离 

res.windowWidth - menuButtonObject.right

2.4完整获取代码: 

let menuButtonObject = wx.getMenuButtonBoundingClientRect();
wx.getSystemInfo({
   success: res => {
     //导航高度
     let statusBarHeight = res.statusBarHeight,
       navTop = menuButtonObject.top,
       navObjWid = res.windowWidth - menuButtonObject.right + menuButtonObject.width, // 胶囊按钮与右侧的距离 = windowWidth - right+胶囊宽度
       navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
     this.globalData.navHeight = navHeight; //导航栏总体高度
     this.globalData.navTop = navTop; //胶囊距离顶部距离
     this.globalData.navObj = menuButtonObject.height; //胶囊高度
     this.globalData.navObjWid = navObjWid; //胶囊宽度(包括右边距离)
     // console.log(navHeight,navTop,menuButtonObject.height,navObjWid)
   },
   fail(err) {
     console.log(err);
   }
 })

组件代码

wxml

<!--components/navbar.wxml-->
<view class="navbar" style="height:{{navHeight+5}}px;background-image:url('{{imgUrl}}') ">
  <!-- 左上角 返回按钮 和 home按钮 wx:if="{{showNav}}" 是控制左上角按钮的显示隐藏,首页不显示 -->
  <view class="navbar_left" style="top:{{navTop}}px;height:{{jnheight-1}}px;width:{{jnwidth-3}}px" wx:if="{{showNav}}">
    <!-- 控制返回按钮的显示 -->
    <view class="navBack" bindtap="navBack">
      <image src="/images/back.png" mode="widthFix"></image>
    </view>
    <!-- home按钮 wx:if="{{showHome}}" 是控制左上角 home按钮的显示隐藏-->
    <view class="nav_line" bindtap="navHome" wx:if="{{showHome}}">
      <image src="/images/index.png" mode="widthFix" style="width:50%"></image>
    </view>
  </view>
  <!-- 中间标题 -->
  <view class="navbar_title" style="top:{{navTop}}px;">{{pageName}}</view>
</view>

js

const App = getApp();
Component({
  // 组件的属性列表
  properties: {
    pageName: String, //中间的title
    showNav: { //判断是否显示左上角的按钮    
      type: Boolean,
      value: true
    },
    showHome: { //判断是否显示左上角的home按钮
      type: Boolean,
      value: true
    },
    imgUrl:{//图片背景路径
      type: String,
      value: App.globalData.imgLink+'index.jpg',
    },
  },
  // 组件的初始数据
  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/index/index'
      })
    },
  }
})

wxss

/* components/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);
}
.navBack image{
  width: 60%;
}
 
.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;
}

app.js

// app.js
App({
  onLaunch() {
    //设置导航栏
    //获取菜单按钮的布局位置信息
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    //获取系统信息
    wx.getSystemInfo({
      success: res => {
        console.log('xxxx', res)
        //状态栏的高度
        let statusBarHeight = res.statusBarHeight,
          //胶囊按钮与顶部的距离
          navTop = menuButtonObject.top,
          navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
        let globalData = this.globalData;
        globalData.navHeight = navHeight;//导航栏高度
        globalData.navTop = navTop;//胶囊按钮与顶部的距离
        globalData.jnheight = menuButtonObject.height;//胶囊的高度
        globalData.jnwidth = menuButtonObject.width;//胶囊的宽度
        globalData.screenHeight = res.screenHeight;//屏幕高度
      },
      fail(err) {
        console.log(err);
      }
    });

  },
  //设置全局对象
  globalData: {
    navHeight: 0,
    navTop: 0,
    jnheight: 0,
    jnwidth: 0,
    screenHeight: 0,
  }
})

去除自带导航

 "navigationStyle": "custom"

引入

{
  "usingComponents":{
    "navbar":"/components/navbar/navbar"
  }
}

使用

<navbar page-name="{{navbar.shoppingName}}"></navbar>
Page({
  data:{
    navbar:{
      shoppingName:'首页',
    },
  }

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值