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 修饰符

相关推荐
anyup12 小时前
分享 5 套 uni-app 实用主题,一键适配暗黑模式
前端·uni-app·视觉设计
gg1593572846016 小时前
Uni-app跨平台开发全解课程:从零基础到企业级多端落地实战
vue.js·uni-app
xshirleyl2 天前
uniapp小兔鲜儿day3
uni-app
Geek_Vison3 天前
2026 跨端框架横评:FinClip、Taro、uni-app、Remax、mPaaS 五款工具技术+业务双维度测评
小程序·uni-app·taro·mpaas·小程序容器
RuoyiOffice3 天前
从 0 到 1 搭建 RuoyiOffice:30 分钟跑通后端+前端+移动端
前端·spring boot·uni-app·开源·oa·ruoyioffice·hrm
Geek_Vison3 天前
APP集成了50多个小程序后,如何搭建一个小程序管理平台来管理这些小程序~
小程序·uni-app·apache·mpaas·小程序容器
梦曦i3 天前
uni-router v1.1.1发布:守卫超时保护+路由监听
前端·uni-app
梦曦i3 天前
全面解析uni-router v1.2.0功能
前端·uni-app
不如摸鱼去4 天前
Wot UI 2.1.0 发布:ConfigProvider 全局配置能力升级
ui·小程序·uni-app
PedroQue994 天前
uni-router:uni-app路由管理新选择
前端·uni-app