微信小程序 - 自定义头部导航栏开发

默认情况下,‌小程序的顶部导航栏由系统自动生成,‌允许修改一些基本样式如背景色、‌文字颜色等。‌然而,‌如果需要实现更复杂的样式,‌如自定义图标、‌背景等,‌或者需要适配不同手机屏幕的高度和样式,‌就需要自定义顶部导航栏。‌

小程序自定义头部导航栏的开发主要涉及系统顶部状态栏和导航栏标题区域,‌自定义顶部导航栏的好处包括提高用户体验、‌增加页面交互性等。‌创建一个自定义导航栏组件,‌包含导航栏的样式和交互逻辑,‌并在需要使用导航栏的页面中引入自定义导航栏组件。‌

如下图,不同手机品牌的顶部区域范围存在差异,这就需要通过提供的相关API获取相应参数,以便根据当前设备屏幕的高度和样式,完成自定义顶部导航栏。

一、app.json参数配置

自定义导航栏首页需要在app.json文件中配置navigationStyle为custom,这样系统默认自动生成的航行栏就消失了。代码如下:

javascript 复制代码
{
  "pages": [
    "pages/index/index",
    "pages/logs/logs",
    "pages/mine/index"
  ],
  "window": {
    "navigationBarTextStyle": "black",
    "navigationBarTitleText": "Weixin",
    "navigationBarBackgroundColor": "#ffffff",
    "navigationStyle": "custom"
  },
  "style": "v2",
  "componentFramework": "glass-easel",
  "sitemapLocation": "sitemap.json",
  "lazyCodeLoading": "requiredComponents"
}

二、自定义导航栏组件

在小程序项目中新建components目录,用于存放自定义组件。在内部创建Header组件,并实现自定义导航栏的样式和交互功能。目录如下图:

首页在index.wxml编写自定义导航栏框架结构,在index.wxss中添加自定义导航栏的样式,index.js中实现导航栏中的交互功能。

index.wxml代码如下:

javascript 复制代码
<!--components/Header/index.wxml-->
<view class="header-wrap">
  <view class="header-fixed-wrap">
    <view class="nav-bar"></view>
    <view class="flex-box">
      <view class="left">
        <button type="default" class="btn-back">返回</button>
      </view>
      <view class="middle">
        <view class="title">标题</view>
      </view>
      <view class="right"></view>
    </view>
  </view>
</view>

css wx

javascript 复制代码
/* components/Header/index.wxss */
.header-wrap { width: 100%; }
.header-wrap .nav-bar{ width: 100%; }
.header-fixed-wrap{ 
    width: 100%; 
    background-color: #ffffff; 
    border-bottom: 1px solid #cccccc; 
    position: fixed; 
    left: 0; 
    top: 0; 
    z-index: 1000; 
}
.header-fixed-wrap .flex-box{ 
    width: 100%; 
    height: 80rpx; 
    display: flex; 
    flex-direction: row; 
    flex-wrap: wrap; 
    justify-content: space-between; 
    align-items: center; 
}
.header-fixed-wrap .left, .header-fixed-wrap .right{ flex: 2; }
.header-fixed-wrap .middle{ flex: 5; }
.header-fixed-wrap .left .btn-back{ 
    width: 80rpx; height: 60rpx; line-height: 60rpx; font-size: 28rpx; font-weight: normal; padding: 0; background: none; 
}
.header-fixed-wrap .middle .title{ 
    width: 80%; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin: 0 auto; 
}
相关推荐
00后程序员张18 小时前
混合 App 怎么加密?分析混合架构下常见的安全风险
android·安全·小程序·https·uni-app·iphone·webview
qq_124987075319 小时前
校园失物招领微信小程序设计与实现(源码+论文+部署+安装)
spring boot·微信小程序·小程序·毕业设计·毕设
梦想要有19 小时前
盲盒小程序有哪些比较不错的功能推荐
小程序
kdniao119 小时前
小程序和电商商家物流查询监控解决方案
大数据·小程序
imooos19 小时前
使用小程序AI推理能力识别车牌号
人工智能·小程序
天呐草莓19 小时前
微信小程序应用开发
python·微信小程序·小程序
2501_9159214320 小时前
Flutter App 到底该怎么测试?如何在 iOS 上进行测试
android·flutter·ios·小程序·uni-app·cocoa·iphone
Slow菜鸟20 小时前
微信小程序(TypeScript)开发指南
微信小程序·小程序·typescript
2501_9159090620 小时前
如何在 Windows 上上架 iOS App,分析上架流程哪些是不用mac的
android·macos·ios·小程序·uni-app·iphone·webview
说私域20 小时前
链动2+1模式、AI智能名片与S2B2C商城小程序在直播营销中的规范化应用研究
人工智能·小程序