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>
相关推荐
2501_915106322 小时前
iOS 抓不到包怎么办?从 HTTPS 代理排查到 TCP 数据流捕获的全链路解决方案
android·tcp/ip·ios·小程序·https·uni-app·iphone
游戏开发爱好者82 小时前
APP上架苹果应用商店经验教训与注意事项
android·ios·小程序·https·uni-app·iphone·webview
济南壹软网络科技有限公司3 小时前
沉浸式体验革命:壹软科技2025新版盲盒源码前端3D渲染与个性化运营技术解析
前端·科技·uni-app·开源·php·盲盒源码
hyswl6664 小时前
数字货物搬家小程序
python·小程序
郭俊强5 小时前
小程序RSA、AES加密
小程序
Qin_jiangshan5 小时前
flutter和reactNative以及uniapp区别
flutter·react native·uni-app
MrTan5 小时前
Uni-App 鸿蒙应用微信相关功能上架踩坑:自制微信安装检测插件
uni-app·harmonyos
2501_916007476 小时前
苹果应用商店上架的系统逻辑,从产品开发到使用 开心上架 上架IPA 交付审核流程
android·ios·小程序·https·uni-app·iphone·webview
IT 前端 张6 小时前
Uni-app 实现全局无操作监听:自动退出弹窗倒计时功能
运维·服务器·uni-app
一只月月鸟呀6 小时前
使用node和@abandonware/bleno写一个ble模拟设备,用Uni-app模拟终端连接
uni-app