小程序 自定义单页面头部导航栏(标题栏)

本文档介绍了如何在小程序中实现自定义单页面头部导航栏,包括在页面JSON配置中添加顶配置项,动态获取并设置状态栏高度,布局与样式调整,以及组件封装和引用的方法。通过创建组件文件并进行相应配置,可以在多个页面中复用该自定义导航栏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写在前:自定义头部标题栏,并封装引用到需要的页面。


实现单页面可用自定义头部(未封装)

首先:根据小程序文档 顶配置项  在需要自定义的页面的json文件中添加下面的配置

{
  "navigationStyle": "custom"
}

其次:为适配各手机顶部状态栏高度高度不同,动态获取高度并存放在全局变量中(放到app.js中)

App({
  onLaunch: function() {

  },
  globalData: {
    statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
  }
})

之后:布局,(注:因为浮动的元素不占位置,所以用空标签来占位置,当然也可以清除浮动的元素带来的影响) 

<view class="topbar" style="padding-top:{{statusBarHeight}}px">
  <view class="arrow-box row">
    <image src="../image/mine_back.png"></image>
  </view>
  <view class="input-box row">
    <input placeholder="请输入你想找的内容" />
  </view>
</view>
<view class="top-comend" style="padding-top:{{statusBarHeight}}px"></view>
<view>这是测试</view>

之后:样式(注:因为设置的是px,效果不是很完美,需自调)

.topbar {
  width: 100%;
  height: 45px;
  /* background-color: pink; */
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
}

.row {
  display: flex;
  justify-content: center;
  align-items: center;
}

.arrow-box {
  width: 10%;
  height: 100%;
  /* background-color: #fe6969; */
  float: left;
}

.arrow-box image {
  width: 14px;
  height: 16px;
  transform: rotateY(180deg);
}

.input-box {
  width: 52%;
  height: 100%;
  /* background-color: #eee; */
  justify-content: center;
  align-items: center;
  float: left;
  margin-left: 3%;
}

.input-box input {
  font-size: 12px;
  color: #333;
  width: 93%;
  height: 30px;
  background-color: #f2f2f2;
  border-radius: 44rpx;
  padding-left: 7%;
}

.top-comend {
  width: 100%;
  height: 45px;
}

最后:因为头部状态栏高度是动态获取的,所以需要单页面引用全局变量

const app = getApp()
Page({
  data: {
    statusBarHeight: app.globalData.statusBarHeight
  },
	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function () {
    // console.log(app.globalData.statusBarHeight);
  },
})

以上是单页面内单独写,以下写成一个自定义组件,各需要的单页面可引用


根据 小程序自定义组件 封装,自定义组件引用

示意图:背景图通栏


 

首先,我们新建四个文件, my-component.js、my-component.json、my-component.wxml、my-component.wxss,

放到文件夹components里,

之后,my-component.wxml:布局

<!-- 这是自定义组件的内部WXML结构 -->
<view class="topbar" style="padding-top:{{statusBarHeight}}px">
  <view class="arrow-box row">
    <image src="../image/mine_back.png"></image>
  </view>
  <view class="input-box row">
    <input placeholder="请输入你想找的内容" />
  </view>
</view>
<!-- <view class="top-comend" style="padding-top:{{statusBarHeight}}px"></view> -->

之后:my-component.wxss:样式

.topbar {
  width: 100%;
  height: 45px;
  /* background-color: pink; */
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
}

.row {
  display: flex;
  justify-content: center;
  align-items: center;
}

.arrow-box {
  width: 10%;
  height: 100%;
  /* background-color: #fe6969; */
  float: left;
}

.arrow-box image {
  width: 14px;
  height: 16px;
  transform: rotateY(180deg);
}

.input-box {
  width: 52%;
  height: 100%;
  /* background-color: #eee; */
  justify-content: center;
  align-items: center;
  float: left;
  margin-left: 3%;
}

.input-box input {
  font-size: 12px;
  color: #333;
  width: 90%;
  height: 30px;
  background-color: #f2f2f2;
  border-radius: 44rpx;
  padding-left: 7%;
  padding-right: 3%;
}

.top-comend {
  width: 100%;
  height: 45px;
}

之后:在自定义组件的 js 文件中,需要使用 Component() 来注册组件,并提供组件的属性定义、内部数据和自定义方法。

const app = getApp()
Component({
  properties: {
    // 这里定义了innerText属性,属性值可以在组件使用时指定
    innerText: {
      type: String,
      value: 'default value',
    }
  },
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    // 这里是一些组件内部数据
    someData: {}
  },
  methods: {
    // 这里是一个自定义方法
    customMethod: function () { }
  }
})

之后:声明这是组件   自定义组件

{
  "component": true
}

以上都是自定义组件各页面,以下是对组件的调用


当前,(我的页面时index.wxml)布局页面调用

<view>
  <component-tag-name inner-text="Some text"></component-tag-name>
</view>
<view class="ceshi"></view>

当前,(我的页面index.wxss)样式页面编写

page {
  width: 100%;
  height: 100%;
}
.ceshi {
   width: 100%;
  height: 100%;
  background-image: url(http://img2.imgtn.bdimg.com/it/u=514294704,4187138556&fm=15&gp=0.jpg);
  background-position: center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  -moz-background-size: 100% 100%;
}

当前,(我的页面index.json)配置页面  (注:"/components/my-component" 路径是我的页面路径配置,与index同级)

{
  "navigationStyle": "custom",
  "navigationBarTextStyle": "white",
  "usingComponents": {
    "component-tag-name": "/components/my-component"
  }
}

当前,(我的页面index.js),因为没有组件通讯(现阶段),所以省略此页面

基本实现自定义页面,后期会继续记录,以免自己忘记

### 小程序自定义头部导航栏的实现方法 #### 1. 配置 `app.json` 文件 在小程序项目的全局配置文件 `app.json` 中,可以通过设置 `"navigationStyle"` 属性为 `"custom"` 来启用自定义导航栏[^1]。如果仅针对特定页面应用此功能,则可以在该页面对应的路径下单独配置: ```json { "pages": [ { "path": "pages/index/index", "style": { "navigationStyle": "custom" } } ] } ``` 对于整个项目统一采用自定义导航栏的情况,可以直接修改 `window` 对象中的属性[^2]: ```json { "window": { "navigationBarTextStyle": "black", "navigationBarTitleText": "uni-app", "navigationBarBackgroundColor": "#F8F8F8", "backgroundColor": "#F8F8F8", "navigationStyle": "custom" } } ``` --- #### 2. 获取设备信息与胶囊按钮位置 由于不同机型的屏幕尺寸和状态栏高度可能存在差异,在开发过程中需动态调整布局以适应各种情况。可以借助以下两个 API 获取所需数据: - **`wx.getMenuButtonBoundingClientRect()`**: 返回右上角胶囊按钮的位置信息,包括其宽高以及距离顶部的距离。 - **`wx.getSystemInfo()` 或 `uni.getSystemInfoSync()` (UniApp)**: 提供当前运行环境下的系统参数,例如状态栏高度。 通过这些接口计算出自定义导航栏的整体高度以及其他相关样式值[^2]: ```javascript // 示例代码:获取设备信息并存储到 data 中 export default { data() { return { statusBarHeight: 0, // 状态栏高度 navBarHeight: 0 // 导航栏总高度 }; }, onLoad() { const menuRect = uni.getMenuButtonBoundingClientRect(); const systemInfo = uni.getSystemInfoSync(); this.statusBarHeight = systemInfo.statusBarHeight; this.navBarHeight = menuRect.bottom + menuRect.top - systemInfo.statusBarHeight; // 计算实际导航栏高度 } }; ``` --- #### 3. 创建自定义导航栏组件 基于以上逻辑创建一个通用的导航栏组件,并将其放置于合适目录(如 `/components/custom-nav-bar`)。以下是具体实现方式[^3]: ##### HTML 结构 ```html <view class="nav-bar"> <!-- 左侧返回图标 --> <view class="back-icon" @tap="handleBack">←</view> <!-- 标题居中显示 --> <view class="title">{{ title }}</view> <!-- 右侧操作区 --> <view class="right-area"></view> </view> ``` ##### CSS 样式 ```css .nav-bar { display: flex; align-items: center; justify-content: space-between; height: var(--nav-height); /* 动态绑定 */ background-color: #ffffff; } .back-icon { margin-left: 10px; font-size: 24px; } .title { text-align: center; flex-grow: 1; font-weight: bold; } .right-area { padding-right: 10px; } ``` ##### JavaScript 行为 ```javascript Component({ properties: { title: String // 接收外部传入的标题内容 }, methods: { handleBack() { uni.navigateBack(); // 返回上一页 } } }); ``` 最后将此组件引入目标页面即可完成调用。 --- ### 总结 综上所述,要实现微信小程序或 UniApp 的自定义头部导航栏,主要分为三个部分工作:一是基础配置更改;二是利用官方提供的工具函数测量界面特性;三是构建灵活可复用的 UI 组件[^1][^2][^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值