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

效果:

在这里插入图片描述

用到的API:

uni.getMenuButtonBoundingClientRect();

官网地址:

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

在这里插入图片描述

控制台打印:

在这里插入图片描述

代码示例:

<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. TCP协议是安全的吗?

    2024-01-24 10:06:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-24 10:06:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-24 10:06:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-24 10:06:02       20 阅读

热门阅读

  1. HCIP-10

    HCIP-10

    2024-01-24 10:06:02      28 阅读
  2. web3简述

    2024-01-24 10:06:02       30 阅读
  3. C#中的委托概念以及例子

    2024-01-24 10:06:02       34 阅读
  4. 2024年回炉计划之动态规划和贪心算法(四)

    2024-01-24 10:06:02       30 阅读
  5. 设置Django中的静态文件

    2024-01-24 10:06:02       34 阅读
  6. GoLang刷题之leetcode

    2024-01-24 10:06:02       33 阅读