小程序自定义顶部导航栏

1.首先在app.json中的window项设置:"navigationStyle": "custom"开启导航栏自定义。

2.components中创建相应文件:

.wxml

<view style="height:{{titleBarHeight}}px;padding-top:{{statusBarHeight}}px;">
<view class="header" style="height:{{titleBarHeight}}px;padding-top:{{statusBarHeight}}px;background:{{bgColor}};">
<view class="title-bar">
<view class="back" wx:if="{{showBack}}" bindtap="headerBack">
<image class="img" src="../../images/back.png"></image>
</view>
</view>
<view class="header-title ellipsis">{{title}}</view>
<block wx:if="{{currPath === 'pages/index/index'}}">
<navigator url="/pages/setting/setting" hover-class="none" class="setting">
<image class="img" src="/image/setting.png"></image>
</navigator>
</block>
</view>
</view>

showBack表示是否显示左上角返回箭头图标

下方的navigator表示如果是首页则左上角显示自定义的图标(如设置)

.js

const app = getApp();
Component({
properties: {
//标题
title: {
type: String,
value: '首页'
},
// 是否显示返回
showBack: {
type: Boolean,
value: false
},
// 背景色
bgColor: {
type: String,
value: '#fff'
}
},
data: {
statusBarHeight: 0,
titleBarHeight: 0,
},
ready: function () {
// 因为很多地方都需要用到,所有保存到全局对象中
if (app.globalData && app.globalData.statusBarHeight && app.globalData.titleBarHeight) {
this.setData({
statusBarHeight: app.globalData.statusBarHeight,
titleBarHeight: app.globalData.titleBarHeight
});
this.emit();
} else {
let that = this
wx.getSystemInfo({
success: function (res) {
if (!app.globalData) {
app.globalData = {}
}
if (res.model.indexOf('iPhone') !== -1) {
app.globalData.titleBarHeight = 44
} else {
app.globalData.titleBarHeight = 48
}
app.globalData.statusBarHeight = res.statusBarHeight
that.setData({
statusBarHeight: app.globalData.statusBarHeight,
titleBarHeight: app.globalData.titleBarHeight
});
that.emit();
},
failure() {
that.setData({
statusBarHeight: 0,
titleBarHeight: 0
});
}
})
}
},
methods: {
headerBack() {
wx.navigateBack({
delta: 1,
fail(e) {
wx.switchTab({
url: '/pages/index/index'
})
}
})
},
emit() {
this.triggerEvent('ready', {
statusBarHeight: app.globalData.statusBarHeight,
titleBarHeight: app.globalData.titleBarHeight
})
},
}
})

properties内是外部传入的数据

titleBarHeight和statusBarHeight分别是导航栏高度和内边距

.wxss

.header {
display: flex;
align-items: center;
top: 0;
position: fixed;
width: 100%;
background-color: #FFF;
z-index: 99999;
}
.header .back {
width: 59rpx;
height: 100%;
display: flex;
align-items: center;
}
.header .title-bar {
padding-left: 22rpx;
height: 56rpx;
width: 100%;
box-sizing: border-box;
}
.header .title-bar .img, .setting .img {
width: 44rpx;
height: 44rpx;
/* vertical-align: top; */
display: block;
}
.header .header-title {
position: absolute;
left: 50%;
font-size: 37rpx;
transform: translateX(-50%);
max-width: 40%;
}
.setting{
position: absolute;
left: 30rpx;
}
/* 一行省略号 */
.ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
word-wrap: normal;
}

.json

{
"component": true,
"usingComponents": {}
}

3.引用

  1. 在其他页面的json文件中设置:

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

  2. 在wxml文件中加入:

    <navbar title="首页"></navbar>

posted @   Li_pk  阅读(801)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示