微信小程序实现自定义头部导航栏(详细)

53 篇文章 35 订阅
43 篇文章 17 订阅

1.实现效果

在这里插入图片描述

2.实现原理

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);
   }
 })

3.代码实现

在json文件中定义custom,自定义导航栏样式.

{
  "usingComponents": {},
  "navigationBarTitleText": "导航栏自定义~",
  "navigationStyle":"custom"
}

拿到相应的导航栏信息

navHeight: app.globalData.navHeight, //导航栏高度
navTop: app.globalData.navTop, //导航栏距顶部距离
navObj: app.globalData.navObj, //胶囊的高度
navObjWid: app.globalData.navObjWid, //胶囊宽度+距右距离

在这里插入图片描述

自定义导航栏的样式,设置fixed布局

<view class="custom_head" style="height:{{navHeight}}px;">
  <view class="flex-row j_b" style="height:{{navObj}}px;padding-top:{{navTop}}px;padding-right:{{navObjWid+5}}px;">
    <view class="head_store text_ellipsis">{{store_name}}</view>
    <picker bindchange="bindPickerChange" value="{{index}}" range="{{storeList}}" range-key="name">
      <view class="flex-row picker">
        <text>切换名称</text>
        <image src="/img/down_arr.png" />
      </view>
    </picker>
  </view>
</view>
.custom_head {
  width: 750rpx;
  background: #7ED1AC;
  color: #fff;
  position: fixed;
  top: 0;
  z-index: 999;
}

.head_store {
  font-size: 30rpx;
  font-weight: bold;
  width: 340rpx;
  margin-right: 30rpx;
  margin-left: 22rpx;
  flex-shrink: 0;
}

picker {
  flex-shrink: 0;
}

.picker {
  font-size: 28rpx;
}

.picker image {
  width: 18rpx;
  height: 10rpx;
  margin-left: 8rpx;
}

4.更多小程序demo,关注苏苏的码云,创造不易,希望大家star+关注,一起学习,冲呀~

  • 4
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
微信小程序中可以通过自定义导航栏实现更加个性化的界面设计。以下是一个简单的实现步骤: 1. 在 app.json 文件中设置 "navigationStyle": "custom",表示使用自定义导航栏。 2. 在页面的 WXML 文件中编写自定义导航栏的 HTML 代码,例如: ``` <view class="navbar"> <view class="navbar-title">自定义导航栏</view> </view> ``` 其中,navbar 表示导航栏的样式,navbar-title 表示标题的样式,可以根据需要进行自定义。 3. 在页面的 wxss 文件中设置导航栏的样式,例如: ``` .navbar { height: 44px; background-color: #fff; border-bottom: 1px solid #ccc; display: flex; align-items: center; justify-content: center; } .navbar-title { font-size: 18px; font-weight: bold; color: #000; } ``` 其中,height 表示导航栏的高度,background-color 表示导航栏的背景颜色,border-bottom 表示导航栏底部的边框线,display、align-items 和 justify-content 表示导航栏标题的布局方式,navbar-title 表示标题的样式,可以根据需要进行自定义。 4. 在页面的 JS 文件中设置导航栏的返回按钮,例如: ``` wx.showModal({ title: '提示', content: '确认返回上一页?', success: function (res) { if (res.confirm) { wx.navigateBack({ delta: 1 }) } } }) ``` 其中,wx.showModal 表示显示一个模态框,title 表示模态框的标题,content 表示模态框的内容,success 表示点击确定按钮后执行的回调函数,wx.navigateBack 表示返回上一页。 通过以上步骤,就可以实现微信小程序自定义导航栏

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值