记一次奇葩的错误,uniapp @tap点击失效

先说问题原因,css 中使用了卡片翻转效果,会将卡片背面翻转隐藏,但是没有完全隐藏起来。使用:transform: translateZ(1px); 将失效元素层级提高,问题解决。

异常说明

下面这段uniapp代码在微信小程序的ios环境能够正常触发tap事件,但是到了安卓没有任何反应,也没报错。

排查过程

(z-index) 首先可能是元素层级导致

很多因为这个的原因但是我遇到的不是。当时代码中元素层级很复杂,排查层级这一步废了很多时间。

事件传递原因

我的组件最外层盒子有注册@touchstart @touchmove @touchend ,有让我在内层tap加 stop 阻止事件传递。试了没效果

解决

结论就是因为使用了3d盒子,然后安卓和ios机制不一样,ios可以触发安卓无法触发。

关键,将这行代码加到点击无法生效样式上。问题得以解决:
transform: translateZ(1px);

css真是高深,玩不明白。

伪代码

javascript 复制代码
<view class="card-swiper">
	<view class="card-swiper-item">
		<view class="card">
			<view class="card-box-base-sty">
				// fun1有效 , fun2无效 (当时排查头发扣完都没想明白!)
				<view @tap="fun1(item)" class="image-card-btn share-btn">按钮2</view>
				<view @tap="fun2(item)" class="image-card-btn share-btn">按钮2</view>
			</view>
			<view class=" card-box-base-sty card-box-back">
			 ....省略
			</view>
		</view>
	</view>
</view>




// style
	.card {
		position: relative;
		width: 70%;
		height: auto;
		transform-style: preserve-3d;
		/* 给卡片添加默认动画 */
		animation: 1s cubic-bezier(0.66, -0.47, 0.33, 1.5) forwards;
		animation-name: rotate-reverse;
	}

	.card-swiper {
		position: relative;
		z-index: 2;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: row;
		transition: transform 0.5s ease;
		/* 保证删除时的平滑重排 */
		will-change: transform;
		backface-visibility: hidden;
	}
	.card-swiper-item {
		flex: none;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		perspective: 1000px;
	}
	// 卡片正面
	.card-box-base-sty {
		position: relative;
		width: 100%;
		height: 100%;
		overflow: hidden;
		border-radius: var(--border-radius);
		padding: 0;
		transition: all 0.5s ease;
		border: 12rpx solid var(--color);
		box-sizing: border-box;
		 // backface-visibility: hidden; // 注意这行 - 罪魁祸首!
	}
	// 卡片背面
	.card-box-back {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		transform: rotateY(180deg);
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 5;
		backface-visibility: hidden;  // 卡片正面注释的放这。
	}

	.image-card-btn{
		transform: translateZ(1px); // 关键生效代码 将z轴层级提高
	}
相关推荐
00后程序员张4 小时前
从审核被拒到稳定过审,iOS 上架技术优化
android·ios·小程序·https·uni-app·iphone·webview
码云社区10 小时前
JAVA二手车交易二手车市场系统源码支持微信小程序+微信公众号+H5+APP
java·开发语言·微信小程序·二手交易·闲置回收
爬坑的小白15 小时前
微信小程序拉起支付
微信小程序·小程序
humors22117 小时前
Deepseek工具:H5+Vue 项目转微信小程序报告生成工具
前端·vue.js·微信小程序·h5·工具·报告
毕设源码-钟学长19 小时前
【开题答辩全过程】以 基于微信小程序的蓝鲸旧物回收系统的设计与实现为例,包含答辩的问题和答案
微信小程序·小程序
洗发水很好用20 小时前
uniapp纯css实现基础多选组件
前端·css·uni-app
sheji341621 小时前
【开题答辩全过程】以 基于微信小程序的考研服务平台为例,包含答辩的问题和答案
微信小程序·小程序
2501_9159184121 小时前
WebKit 抓包,WKWebView 请求的完整数据获取方法
android·前端·ios·小程序·uni-app·iphone·webkit
AnalogElectronic1 天前
uniapp学习1,hello world 项目,打包到微信小程序,贪吃蛇小游戏
学习·微信小程序·uni-app
雪芽蓝域zzs1 天前
uniapp 真机上传图片提示打包未添加Camera模块
android·uni-app