uniapp 自定义小程序头部

2,384 阅读1分钟

获取胶囊位置信息

getMenuButtonBoundingClientRect()方法

在小程序平台,如果原生导航栏被隐藏,仍然在右上角会有一个悬浮按钮,微信下也被称为胶囊按钮。本API用于获取小程序下该菜单按钮的布局位置信息,方便开发者布局顶部内容时避开该按钮。

坐标信息以屏幕左上角为原点。

平台差异说明 | App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 字节跳动小程序 | QQ小程序 | | :-: | :-: | :-: | :-: | :-: | :-: | :-: | | x | x | √ | x | √| √ | √ |

pages.jons配置

  1. 配置局部或者全局自定义头部

组件自定义

<template>
	<view class='w-header-fixed w-row w-justify-content-center w-align-items-center' :style="{paddingTop:top+'px',height:height+'px'}">
		<view class="w-header-tt">{{title}}</view>
	</view>
</template>

<script>
	
	export default {
		data() {
			return {
				top: 0,
				height: 0
			}
		},
		props: {
			title: {
				type: String,
				default: ''
			}
		},
		created() {
			this.getMenuButtonBoundingClientRect();
		},
		methods: {
			getMenuButtonBoundingClientRect() {
				let info = uni.getMenuButtonBoundingClientRect();
				console.log(info);
				this.top = info.top;
				this.height = info.height;
			}
		}
	}
</script>

<style lang="scss">
.w-header-fixed {
	position: fixed;
	left: 0;
	width: 100%;
	padding-bottom: 20upx;
	background: #999;
	color: white;
	.w-header-tt {
		text-align: center;
	}
}
</style>