uniapp解决点击穿透问题总结

摘要:

今天uniapp写详情页的时候遇到点击跳转详情与点击跳转导航既然穿透出去了,点击导航跳转详情页了!

bash 复制代码
<view class="card_item" v-for="item in status" :key="item.value">
    <view @tap="toDetails(item)">
        <view class="d-f">
            <text class="f32 fb mb10">XXX</text>
            <uv-icon @tap="handlePhoneClick($event)" size="20" class="ml30 mr30" name="/static/phone_icon.png"></uv-icon>
            <uv-icon @tap="handleNavigationClick($event)" size="20" name="/static/nav_icon.png"></uv-icon>
        </view>
     </view> 
</view>
typescript 复制代码
<script setup>
  import { ref } from "vue";
  const toDetails = (t) => {
    const now = Date.now();
    if (now - lastActionTime < 500) { // 防止快速连续点击
      return;
    }
    lastActionTime = now;
    
    uni.navigateTo({
      url: `/details/index1?id=${t.type}`,
    });
  };

  const toPhoneCall = () => {
    const now = Date.now();
    if (now - lastActionTime < 500) { // 防止快速连续点击
      return;
    }
    lastActionTime = now;
    uni.makePhoneCall({
      phoneNumber: '123456'
    });
  }

  const toNavigation = (t) => {
    const now = Date.now();
    if (now - lastActionTime < 500) { // 防止快速连续点击
      return;
    }
    lastActionTime = now;
    
    uni.navigateTo({
      url: `/navigation/index?id=123`,
    });
  };

  const handlePhoneClick = (event) => {
    event?.preventDefault?.();
    event?.stopPropagation?.();
    toPhoneCall();
  };

  const handleNavigationClick = (event) => {
    event?.preventDefault?.();
    event?.stopPropagation?.();
    toNavigation();
  };
</script>

一开始使用@click,穿透是绝对存在的!

后来添加@click.stop阻止事件冒泡,还是不行!

手动阻止冒泡还是不行!

bash 复制代码
const toNavigation = (event) => {
  event.stopPropagation(); // 阻止事件冒泡
  uni.navigateTo({
    url: `/navigation/index?id=123`,
  });
};

@click.prevent.stop还是不行!

CSS 防止事件穿透还是不行!

css 复制代码
.card_info {
  pointer-events: none; // 禁用整个区域的指针事件
  
  .d-f { // 只对需要交互的元素启用指针事件
    pointer-events: auto;
  }
}

.d-f {
  pointer-events: auto; // 确保图标可以响应点击
}

最终使用 @tap 替代 @click - 在移动端应用中,@tap 通常是更好的选择

添加防重击措施 - 通过 lastActionTime 防止快速连续点击

安全的事件处理 - 使用 event?.preventDefault?.() 安全链式调用

移除了可能导致冲突的 stop 修饰符

相关推荐
千逐6818 小时前
Uniapp 鸿蒙实战之 AtomGit APP - 仓库详情与文件树浏览
服务器·microsoft·uni-app
万物得其道者成19 小时前
uni-app App 热更新实现指南:从接口检查到下载安装重启(附带AI提示词)
uni-app
anyup20 小时前
uView Pro 正式支持 MCP 协议!让 AI 秒懂你的组件库
前端·uni-app·ai编程
千逐681 天前
Uniapp 鸿蒙实战之 AtomGit APP - 首页设计与仓库列表实现
华为·uni-app·harmonyos
天丁o2 天前
Spring Boot + uni-app 智慧考勤闭环 Demo:打卡记录、异常状态和日统计如何复用到企业系统
spring boot·uni-app·mybatis plus·企业管理系统·考勤系统
这是个栗子2 天前
uni-app 微信小程序开发:常用事件指令(@xxx)(一)
微信小程序·小程序·uni-app
小徐_23335 天前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app
宸翰7 天前
解决 uni-app App 端 vue-i18n 占位符丢失:封装跨端可用的 tf 格式化方法
前端·vue.js·uni-app
时光足迹8 天前
uni-app 视频通话实战:康复师与患者视频问诊的 6 个致命 Bug 与解决方案
android·ios·uni-app
时光足迹8 天前
腾讯云 TRTC UniApp SDK 从入门到上线
前端·vue.js·uni-app