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>
相关推荐
帅帅的记忆15 小时前
uniapp项目调用原生android studio的sdk文件(即aar文件)
android·uni-app·android studio
宠友信息16 小时前
Spring Boot与Redis ZSet实现仿小红书源码双列瀑布流推荐
java·大数据·前端·spring boot·redis·mysql·uni-app
投票竞赛18 小时前
6 类细分场景在线投票工具,微信小程序亲测测评精准匹配对应评选需求
小程序
这是个栗子1 天前
uni-app微信小程序开发:高频核心 API(三)
微信小程序·小程序·uni-app
quweiie2 天前
thinkphp8结合jwt与微信小程序接口鉴权
微信小程序·小程序·thinkphp接口·接口鉴权
黄华SJ520it3 天前
预约上门系统开发:懒人经济下的商业机遇与技术实践
小程序·系统开发
软件技术新观察3 天前
2026年北京教育医疗小程序与APP定制开发:十大服务商实力测评
大数据·小程序
万岳科技程序员小金3 天前
真人数字人小程序如何开发?AI数字人平台搭建流程全面解析
人工智能·小程序·ai数字人系统源码·ai数字人平台搭建·ai数字人小程序开发
言乐63 天前
Python实现可运行解密游戏游戏框架
python·游戏·小程序·游戏程序·关卡设计
大白要努力!4 天前
uni-app 全面入门速查手册
前端·uni-app