uni-app:踩坑路---scroll-view内使用fixed定位,无效的问题

前言:

emmm,说起来这个问题整得还挺好笑的,本人在公司内,奋笔疾书写代码,愉快的提交测试的时候,测试跟我说,在苹果手机上你这个样式有bug,我倒是要看看,是什么bug。

安卓vs苹果

ok,我相信已经看出了差异了,安卓的遮罩层正常显示,而苹果的遮罩层只在我的绿色框内,被截断了,我赶忙看代码:

CustomItem.vue:自定义组件

蓝色的正方形,外加上一个遮罩层,点击蓝色方块的时候,显示遮罩层,遮罩层内写我的要展示的一些内容。

复制代码
<template>
	<view class="">
		<view class="item" @click="visible = true"></view>

		<view class="mask" v-if="visible" @click="visible = false"></view>
	</view>
</template>

<script>
export default {
	name: 'CustomItem',
	data() {
		return {
			visible: false
		};
	}
};
</script>

<style lang="scss" scoped>
.item {
	width: 100rpx;
	height: 100rpx;
	background-color: #00aaff;
}
.mask {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: rgba(#000, 0.5);
}
</style>

父组件:引用CustomItem组件;

复制代码
<template>
	<view class="index">
		<scroll-view scroll-x class="scroll">
			<view class="list">
				<custom-item class="item" v-for="i in 10" :key="i"></custom-item>
			</view>
		</scroll-view>
	</view>
</template>

<script>
import CustomItem from '@/components/CustomItem/index.vue';
export default {
	components: {
		CustomItem
	}
};
</script>

<style lang="scss" scoped>
.index {
	width: 100vw;
	height: 100vh;
	display: flex;
	align-items: center;
	justify-content: center;
}

.scroll {
	width: 400rpx;
	height: 150rpx;
	background-color: #aaaa7f;
}
.list {
	padding: 20rpx;
	display: flex;
	align-items: center;
	.item {
		margin-right: 20rpx;
	}
}
</style>

如此:就造成了上面的结果,在ios上显示不正常;于是我立马进入百度啦,问心一言啦,通义千问啦,最后哈,在社区找到了这么一个帖子;帖子看这里

emmm,寻求解决办法:

1.弃用scroll-view,改用view,使用css滚动;

复制代码
<view class="list">
	<custom-item class="item" v-for="i in 10" :key="i"></custom-item>
</view>

<style lang="scss" scoped>
.list {
	width: 400rpx;
	height: 150rpx;
	background-color: #aaaa7f;
	padding: 20rpx;
	overflow-x: scroll;
	display: flex;
	align-items: center;
	.item {
		margin-right: 20rpx;
	}
}
</style>

2.如非必要,可以更改接口,其实上面的自定义组件CustomItem看着好像没有什么问题,是机型系统差异导致的,但是我们也并不能将全部原因归结于系统。

当我们把它合起来看的话,就会发现在结构上似乎有一些问题了,遮罩层这一块的元素就需要循环10次,如果列表很长的话,那不就妥妥增加了很多的dom,浪费性能不说,结构设计也看着很怪, 所以有时候我们在封装组件的时候,不妨也这样考虑一下,可能这么写真的不太合适,最好的方案就是再划分下结构,只需要记得mask内的元素在放在scroll-view的外层即可!

html 复制代码
<scroll-view scroll-x class="scroll">
	<view class="list">
		<view class="" v-for="i in 10" :key="i">
			<view class="item" @click="visible = true"></view>
			<view class="mask" v-if="visible" @click="visible = false"></view>
		</view>
	</view>
</scroll-view>

告辞!

相关推荐
2501_915918414 分钟前
Mac 抓包软件有哪些?Charles、mitmproxy、Wireshark和Sniffmaster哪个更合适
android·ios·小程序·https·uni-app·iphone·webview
2501_915106327 分钟前
iOS 抓包绕过 SSL 证书认证, HTTPS 暴力抓包、数据流分析
android·ios·小程序·https·uni-app·iphone·ssl
WeiAreYoung8 分钟前
uni-app xcode 制作iOS Notification Service Extension 远程推送图文原生插件
ios·uni-app·xcode
2501_915921438 小时前
iOS App 电耗管理 通过系统电池记录、Xcode Instruments 与克魔(KeyMob)组合使用
android·ios·小程序·https·uni-app·iphone·webview
2501_9159184119 小时前
iOS App 测试方法,Xcode、TestFlight与克魔(KeyMob)等工具组合使用
android·macos·ios·小程序·uni-app·iphone·xcode
2501_9159214320 小时前
iOS 描述文件制作过程,从 Bundle ID、证书、设备到描述文件生成后的验证
android·ios·小程序·https·uni-app·iphone·webview
2501_915909061 天前
如何保护 iOS IPA 文件中资源与文件的安全,图片、JSON重命名
android·ios·小程序·uni-app·json·iphone·webview
2501_915909062 天前
原生与 H5 共存情况下的测试思路,混合开发 App 的实际测试场景
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者82 天前
了解 Xcode 在 iOS 开发中的作用和功能有哪些
android·ios·小程序·https·uni-app·iphone·webview
2501_915106322 天前
iOS 抓包工具实战实践指南,围绕代理抓包、数据流抓包和拦截器等常见工具
android·ios·小程序·https·uni-app·iphone·webview