uniapp小程序实现自定义返回按钮和胶囊对齐 做到兼容各手机型号

效果:

用到的API:

javascript 复制代码
uni.getMenuButtonBoundingClientRect();

官网地址:

https://uniapp.dcloud.net.cn/api/ui/menuButton.html#getmenubuttonboundingclientrect

控制台打印:

代码示例:

javascript 复制代码
<template>
	<view class="container">
		<!-- 返回按钮 -->
		<view class="back" :style="{top: top}" @click="goBack()">
			<u-icon name="arrow-left"></u-icon>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				top: 0
			};
		},
		mounted() {},
		created() {
			console.log('getMenuButtonBoundingClientRect===>', uni.getMenuButtonBoundingClientRect());

			//让自定义导航栏头部组件始终和胶囊对齐 做到兼容各手机型号
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
			this.top = menuButtonInfo.top + 'px';
		},
		methods: {
			goBack() {
				uni.navigateBack();
			}
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		// 背景图url 可替换
		background-image: url('https://e-computer.xxxx.com:12480/miniPic/company/create2.jpg');
		width: 100%;
		height: 100%;
		background-size: cover;
		background-repeat: no-repeat;
		background-position: center center;
		position: relative;

		.back {
			text-align: center;
			width: 50rpx;
			height: 50rpx;
			border-radius: 50%;
			position: absolute;
			left: 30rpx;
			line-height: 50rpx;
			opacity: 0.7;
			font-size: 32rpx;
			background: #e2e2e2;
		}
	}
</style>
相关推荐
不法14 小时前
uniapp map地图导航/地图路线
前端·vue.js·uni-app
黄华SJ520it15 小时前
线上线下多门店融合系统开发指南
小程序·系统开发
一孤程1 天前
Airtest自动化测试第五篇:小程序与Web测试——跨平台自动化全覆盖
前端·自动化测试·小程序·自动化·测试·airtest
CRMEB定制开发2 天前
开源商城源码下载后怎么搭建?五步走通完整流程
小程序·开源·商城系统·私域运营
淡淡的幼稚2 天前
【YFIOs】从传感芯片到表格曲线
uni-app·.net
宠友信息2 天前
多端即时通讯源码技术实践,Redis路由、MySQL持久化与Socket接入
spring boot·websocket·mysql·uni-app
小羊Yveesss2 天前
2026年如何自己搭建一个外贸网页?单页验证和完整网站的区别
微信小程序·小程序·开源
weishuangyun1232 天前
怎么开发自己的小程序?2026 三种主流方式实测对比
小程序
宠友信息2 天前
Spring Boot与异步审核构建仿小红书源码内容发布全流程
java·数据库·spring boot·redis·mysql·oracle·uni-app
蜡台2 天前
uniapp vue2 转 vue3
前端·javascript·uni-app·vue3·vue2