uniapp小程序获取胶囊按钮高度自定义顶部标题栏

前言

小程序自带的顶部不太好康,而且能改的只要颜色和文字。很多时候设计图都是需要自定义顶部的。比如放个搜索框 或者 标题 或者 可切换的 tabs 等等 。分享一下我之前的做法。



一、获取胶囊的位置信息 API

wx.getMenuButtonBoundingClientRect()

基础库 2.1.0 开始支持,低版本需做兼容处理

小程序插件:支持,需要小程序基础库版本不低于 2.15.0

微信 Windows 版:支持

微信 Mac 版:支持

获取菜单按钮(右上角胶囊按钮)的布局位置信息。坐标信息以屏幕左上角为原点。

1.返回值

Object

返回菜单按钮的布局位置信息

属性类型说明
widthnumber宽度,单位:px
heightnumber高度,单位:px
topnumber上边界坐标,单位:px
rightnumber右边界坐标,单位:px
bottomnumber下边界坐标,单位:px
leftnumber左边界坐标,单位:px

二、演示

标题型

在这里插入图片描述

1.关闭原生的顶部栏

/pages.json

"pages": [{
			"path": "pages/index/index",
			"style": {
				"navigationStyle": "custom",
				"app-plus": {
					"titleView": false
				}
			}
		},
        ...
 }]

2.页面加载时获取胶囊高度并设置标题高度

index.vue

<template>
	<view class="bg">
		<view class="title" :style="'padding-top:' + titleHeight + 'px'">新华的湖</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			titleHeight: 0
	},
	onLoad() {
		// #ifdef MP-WEIXIN
		this.getHeight();
		// #endif
	},
	methods: {
		// 获取微信右上角胶囊高度
		getHeight() {
			let res = wx.getMenuButtonBoundingClientRect();
			this.titleHeight = res.top;
		}
	}
};
</script>

<style lang="scss" scoped>
.title {
	font-weight: 600;
	line-height: 30px;
	margin-left: 40rpx;
	font-size: 17px;
}
</style>

三、举例

不想翻以前的代码,拿图说明一下,做法与上面相似滴。聪明的你肯定轻松做出来啦~

1.tabs

uview有现成可以用的tabs 标签,可以根据当前索引来实现切换效果

下面是我去年用 js 写的效果,点击规则后会下拉到规则那里,点击公告回到最顶部 (第一张图没截好,下划线是在公告那的)

打开主页切换到规则

2.设置背景图

去掉顶部栏后,可以把图片设置到全屏,十分美观,在app里面可以很实用。小程序的话,如果有背景图上有文字,尽量考虑与胶囊按钮对齐,这样看起来正常点

打开主页切换到规则

结语

可以关注一下我滴个人博客网站哟 www.dddhl.cn
源码已经分享到 gitee 上了。

你可以在uni-app中使用自定义导航栏组件来修改导航栏胶囊样式。首先,你需要在页面配置文件(如pages.json或者使用页面生命周期函数)中设置navigationStyle为custom。然后在自定义导航栏组件中使用uni.getSystemInfoSync()获取设备信息,计算出状态栏高度和导航栏高度,并设置胶囊的样式。以下是一个简单的示例代码: ``` <template> <view class="nav"> <view class="status-bar" :style="{height: statusBarHeight + 'px'}"></view> <view class="nav-bar" :style="{height: navBarHeight + 'px'}"> <view class="nav-bar__left" :style="{width: capsuleLeft + 'px'}"></view> <view class="nav-bar__title">{{title}}</view> <view class="nav-bar__right" :style="{width: capsuleRight + 'px'}"></view> </view> </view> </template> <script> export default { data() { return { title: '', statusBarHeight: 0, navBarHeight: 0, capsuleLeft: 0, capsuleRight: 0 } }, onLoad() { // 获取设备信息 const systemInfo = uni.getSystemInfoSync() const capsuleInfo = uni.getMenuButtonBoundingClientRect() // 计算状态栏高度和导航栏高度 this.statusBarHeight = systemInfo.statusBarHeight this.navBarHeight = capsuleInfo.top + (capsuleInfo.height - this.statusBarHeight) * 2 // 计算胶囊的位置和大小 this.capsuleLeft = capsuleInfo.left - systemInfo.screenWidth / 2 + capsuleInfo.width / 2 this.capsuleRight = systemInfo.screenWidth - this.capsuleLeft - capsuleInfo.width } } </script> <style> .nav { width: 100%; height: var(--nav-height); background-color: #fff; position: fixed; top: 0; left: 0; z-index: 999; } .status-bar { width: 100%; background-color: #fff; } .nav-bar { display: flex; align-items: center; justify-content: space-between; padding: 0 16px; box-sizing: border-box; } .nav-bar__left, .nav-bar__right { width: 44px; height: 100%; } .nav-bar__title { font-size: 18px; font-weight: bold; text-align: center; } </style> ``` 你可以根据自己的需求修改样式和布局。同时,也可以在组件中添加一些额外的功能,例如返回按钮、搜索框等等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

holden丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值