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>
相关推荐
说私域1 小时前
基于开源链动 2+1 模式 AI 智能名片 S2B2C 商城小程序的社群团购品牌命名策略研究
人工智能·小程序·开源·零售
源码_V_saaskw5 小时前
场馆预定系统小程序PHP+uniapp
微信小程序·小程序·uni-app·php
七月shi人5 小时前
用claude3.7,不到1天写了一个工具小程序(11个工具6个游戏)
前端·小程序·ai编程
ml130185288746 小时前
开发一个小程序需要多久时间?小程序软件开发周期
java·小程序·开源软件·软件需求
小徐_23336 小时前
uni-app工程实战:基于vue-i18n和i18n-ally的国际化方案
前端·微信小程序·uni-app
说私域7 小时前
基于开源 AI 大模型 AI 智能名片 S2B2C 商城小程序的京城首家无人智慧书店创新模式研究
人工智能·小程序·开源·零售
前端小趴菜057 小时前
UniApp Vue 3 中的网络请求封装及用法
前端·vue.js·uni-app
然后就去远行吧9 小时前
小程序API —— 57 拓展 - 增强 scroll-view
小程序
Fri_11 小时前
微信小程序 request 流式数据处理
微信小程序·小程序
wocwin11 小时前
uniapp微信小程序基于wu-input二次封装TInput组件(支持点击下拉选择、支持整数、电话、小数、身份证、小数点位数控制功能)
微信小程序·uni-app