微信小程序自定义导航栏效果
作者:XJ_18335388352 发布时间:2024-07-05 14:08:32
标签:微信小程序,导航栏
本文实例为大家分享了微信小程序自定义导航栏的具体代码,供大家参考,具体内容如下
第一步 添加属性 “navigationStyle”: “custom”
全局: app.json中添加属性 “navigationStyle”: “custom”
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#fff",
"navigationBarTitleText": "WeChat",
"navigationBarTextStyle": "black",
"navigationStyle": "custom"
},
局部: 单个文件index.json中添加属性 “navigationStyle”: “custom”
{
"navigationBarTitleText": "我的",
"navigationBarTextStyle": "white",
"navigationStyle": "custom",
"usingComponents": {}
}
第二步 自定义导航栏
获取设备顶部窗口的高度(不同设备窗口高度不一样,根据这个来设置自定义导航栏的高度)
app.js
// app.js
App({
onLaunch() {
//自定义导航栏 获取设备顶部窗口的高度(不同设备窗口高度不一样,根据这个来设置自定义导航栏的高度)
wx.getSystemInfo({
success: (res) => {
let custom = wx.getMenuButtonBoundingClientRect()
this.globalData.statusBarHeight = res.statusBarHeight
this.globalData.navBarHeight = custom.height + (custom.top - res.statusBarHeight) * 2
}
})
},
globalData: {
statusBarHeight: 0,
navBarHeight: 0,
}
})
第三步 自定义组件
navbar.js
const app = getApp()
Component({
// multipleSlots 为组件开启多插槽模式
options: {
multipleSlots: true,
},
// externalClasses 为组件指定多个外部样式类
externalClasses: ['nav-bgc-class', 'nav-title-class', 'ex-back-pre'],
// properties 组件用来储存外部数据
properties: {
navbarData: { //navbarData 由父页面传递的数据,变量名字自命名
type: Object,
value: {},
observer: function (newVal, oldVal) { }
},
},
// 组件用来储存内部私有数据
data: {
// 自定义导航栏的高度
statusBarHeight: app.globalData.statusBarHeight,
navBarHeight: app.globalData.navBarHeight,
},
// attached函数 当组件进入页面节点树时触发,可以使用setData,绝大多数初始化工作在这个时机进行
attached: function () { },
// methods对象 定义组件内的各种方法
methods: {
// 返回键,触发自定义事件,将返回的事件交给父级页面来分情况定义
_navback() {
// this.triggerEvent('goBack')
wx.navigateBack()
}
}
})
navbar.json
{
"usingComponents": {},
"component": true
}
navbar.wxml
<!-- 默认为黑色的返回键-->
<view class="nav-wrap nav-bgc-class" style='height: {{statusBarHeight + navBarHeight}}px;'>
<!-- 左上角的返回按钮 其中wx:if='{{navbarData.showCapsule}}' 是控制左上角按钮的显示隐藏,1为显示,0为隐藏 -->
<view class='nav-capsule' style='margin-top: {{statusBarHeight}}px; height: {{navBarHeight}}px;' wx:if='{{navbarData.showCapsule}}' bindtap='_navback'>
<image class='back-pre ex-back-pre' src='back.png' mode='aspectFill'></image>
</view>
<!-- 中间的标题 -->
<view class='nav-title nav-title-class' style='margin-top: {{statusBarHeight}}px; height: {{navBarHeight}}px;line-height: {{navBarHeight}}px;'>{{navbarData.title}}</view>
</view>
navbar.wxss
/* 顶部固定定位 标题要居中 自定义按钮和标题要和右边微信原生的胶囊上下对齐 */
.nav-wrap {
position: fixed;
width: 750rpx;
top: 0;
left: 0;
right: 0;
background: #fff;
z-index: 9999999;
transform: translate3d(0, 0, 0);
}
/* 返回键 */
.nav-capsule {
width: 140rpx;
/* background-color: skyblue; */
/* 让里面的图片元素垂直居中 */
display: flex;
align-items: center;
margin-left: 30rpx;
}
.back-pre {
width: 32rpx;
height: 36rpx;
}
/* 标题 */
.nav-title {
position: absolute;
left: 0;
right: 0;
bottom: 0;
width: 300rpx;
margin: auto;
/* 水平垂直居中 */
/* display: flex; */
align-items: center;
justify-content: space-around;
/* 超出部分省略号显示 */
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
/* 字体设置 */
font-family: PingFangSC-Medium;
font-size: 32rpx;
color: #000000;
letter-spacing: 0;
text-align: center;
}
第四步 使用组件
新建一个文件夹mine
mine.js
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
nvabarData: {
showCapsule: 0, //是否显示左上角图标 1表示显示 0表示不显示
title: '我的', //导航栏 中间的标题
},
height: app.globalData.statusBarHeight + app.globalData.navBarHeight,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
var userInfo = wx.getStorageSync('userInfo')
if (userInfo) {
this.setData({
userInfo: userInfo,
hasUserInfo: true
})
}
var phoneNumber = wx.getStorageSync('phoneNumber')
if (phoneNumber) {
this.setData({
phoneNumber: phoneNumber,
})
}
var loginInfo = wx.getStorageSync('loginInfo')
if (loginInfo) {
this.setData({
loginInfo: loginInfo
})
}
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
mine.json
{
"navigationStyle": "custom",
"usingComponents": {
"nav-bar": "/components/navbar/navbar",
}
}
mine.wxml
<!-- navbar头部 -->
<nav-bar navbar-data='{{nvabarData}}'></nav-bar>
<view style='margin-top: {{height}}px;'>
内容
</view>
第五步 看效果
来源:https://blog.csdn.net/XJ_18335388352/article/details/120428336


猜你喜欢
- 准备工作下载python,本文以python3.6为例。python3.6下载地址:python3下载地址,选择合适的版本安装。安装成功后,
- <?php $foo = 'Bob'; // 将 'Bob' 赋给 $foo $bar = &
- 1,exists和in的理解exists:如果子查询中包括某一行,那么就为TRUE in:如果操作数为TRUE等于表达式列表中的一个,那么就
- 很简单,我们用两个文件就可以实现这一功能:login.htm<% @ Language=JavaScript&nb
- 本文实例总结了GO语言基本数据类型。分享给大家供大家参考。具体如下:1、注释(与C++一样)行注释://块注释:/* ...*/2、标识符可
- Bootstrap是网上最流行的前端开发框架. 除了用它,我不知道还有其他更快的方法去构建一个响应式的网站。但是自从我向网页添加动态功能的工
- 脚本主要功能:1)通过zabbix api接口采集所有监控主机ip地址;2)通过cmdb系统(蓝鲸)接口采集所有生产主机IP地址、主机名、操
- Matlab常用的输出命令1、disp方法(1)方法(2)方法(3)需要注意:直接加数字不会显示数字,num2str()使数值转换为字符串类
- 枚举是常用的功能,看看Python的枚举.from enum import EnumMonth = Enum('Month'
- 本文实例讲述了Thinkphp5.0 框架实现控制器向视图view赋值及视图view取值操作。分享给大家供大家参考,具体如下:Thinkph
- 上一篇的DOCTYPE声明好以后,接下来的代码是:<html xmlns="xhtml" ta
- 目录一、环境准备二、问题分析三、spider四、item五、setting六、pipelines七、middlewares八、使用jupyt
- 如果在prop中传的值为一个没有使用特殊命名规则的变量如:(type),可以顺利传值:<code class="langua
- vue-cli使用stimulsoft.reports.js(保姆级教程)第一部分:数据源准备以下是JSON数据的教程json数据结构{&q
- 如 现有字符串 "[]aseabcd[12345]ddxabcdsx[]",要截取"abcd[" 和
- re.search():匹配整个字符串,并返回第一个成功的匹配。如果匹配失败,则返回None pattern: 匹配的规则,str
- 如何向 pandas.DataFrame 添加新的列或行通过指定新的列名/行名来添加,或者用pandas.DataFrame的assign(
- 序言本文所提及的VTD-XML并非本文作者原创,作者只是对它进行介绍。问题通常当我们提起XML的使用时,最头痛的部分便是XML的verbos
- centos7之Python3.74安装安装版本:Python3.74系统版本:centos7系统默认安装Python2.7,保留。安装/u
- Go语言中 sync 包里提供了互斥锁 Mutex 和读写锁 RWMutex 用于处理并发过程中可能出现同时两个或多个协程(或线程)读或写同