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>
相关推荐
小徐_23331 小时前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!
前端·uni-app·echarts
iOS阿玮5 小时前
永远不要站在用户的对立面,挑战大众的公知。
uni-app·app·apple
xw57 小时前
uni-app中v-if使用”异常”
前端·uni-app
!win !7 小时前
uni-app中v-if使用”异常”
前端·uni-app
2501_9159184110 小时前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张11 小时前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
破无差21 小时前
《赛事报名系统小程序》
小程序·html·uniapp
00后程序员张1 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬1 天前
uniapp-微信小程序分享功能-onShareAppMessage
微信小程序·小程序·uni-app
2501_915106321 天前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode