移动端点击穿透问题

x33g5p2x  于2022-02-16 转载在 其他  
字(3.4k)|赞(0)|评价(0)|浏览(209)

一、前言
我们在上篇博客中了解到移动端点击事件存在延迟问题,我们可以通过一些措施,来解决300ms延迟问题。此时我们想一下,click事件存在延时300ms问题,我们在移动端使用touchstart事件来监听不可以解决吗?
移动端的touchstart事件也存在一些弊端:
1、如果我们想要滑动手机屏幕,但是此时也是会触摸到手机,从而触发touchstart事件
2.、touchstart事件存在点击穿透的问题。
二、什么是点击穿透
如果我们存在两个元素A,B,此时元素B在元素A的上层,我们监听元素B的touchstart事件,当触发事件时,我们会将B进行隐藏。随后元素A就会触发click事件,如果元素A为一个链接,则元素A就会实现跳转。原因是:当触发元素B的touchstart事件时, 触发事件的流程为:touchstart > touchend > click,此时元素B消失,浏览器会在300ms后将click事件派发给相同位置的元素。如果此时元素A在元素B触发touchstart的相同位置,此时会触发元素A的click事件。

<!DOCTYPE html>
<html lang="cn">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="user-scalable"> 
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    
    .box1 {
      width: 100%;
      height: 500px;
      background-color: red;
    }
    .box2 {
      width:100%;
      height: 200px;
      background-color: green;
      position: absolute;
      z-index: 1111;
      top: 0px;
    }

  </style>
  <title>Document</title>
</head>
<body>
  
  <div class="box1">
    <a href="https://www.baidu.com">百度一下 你就知道</a>
    <div class="box2"></div>
    
  </div>
  
  <script>

    const box1 = document.querySelector(".box1")
    const box2 = document.querySelector(".box2")
    box2.addEventListener("touchstart", (e) => {
      console.log("触发box2哈哈哈哈")
      box2.style.display = "none"
    })

    box1.addEventListener("click", () => {
      console.log("触发box1的哈哈哈")
    })
  </script>

</body>
</html>

三、浏览器事件触发顺序

<!DOCTYPE html>
<html lang="cn">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    * {
      margin: 0px;
      padding: 0px;
    }
    #app {
      width: 100%;
      height: 100px;
      background-color: red;
    }
  </style>
  <title>Document</title>
</head>

<body>

  <div id="app"></div>

  <script>

    const app = document.querySelector("#app")
    app.addEventListener("touchstart", () => {
      console.log("触发touchstart事件")
    })
    app.addEventListener("mouseover", () => {
      console.log("触发mouseover事件")
    }) 
    app.addEventListener("mousemove", () => {
      console.log("触发mousemove事件")
    })
    app.addEventListener("mousedown", () => {
      console.log("触发mousedown事件")
    })
    app.addEventListener("mouseup", () => {
      console.log("触发mouseup事件")
    })
    app.addEventListener("touchmove", () => {
      console.log("触发touchmove事件")
    })
    app.addEventListener("touchend", () => {
      console.log("触发touchend事件")
    })
    app.addEventListener("click", () => {
      console.log("触发click事件")
    })
  </script>

</body>

</html>

上面为我测试的浏览器中的事件执行顺序,此时顺序的排列为:
touchstart > touchend > mouseover > mousemove > mousedown > mouseup > click
touchstart > touchmove > touchend

事件描述触发时机
touchstart开始触摸手指触屏立即触发
touchmove移动或者拖拽取决于系统和浏览器
touchend触摸结束手指离开立即触发

四、出现的现象
1、点击穿透问题
触发蒙层下的touchstart方法,蒙层消失,触发该位置下的click事件。
2、跨页面点击穿透问题
和上述原理一致,就是下面存在一个a链接,当触发上层的touchstart方法,就会触发同位置的click事件,如果a链接在当前触发位置的下方,则就会触发页面跳转。
五、解决方案
1、将所有的click事件全部改为touch事件
因为一些a链接使用的是click事件,此时可以通过touchend和span标签结合使用。

2、不使用touch事件,只使用click事件
在移动端浏览器中只使用click事件会导致300ms延时的问题,如果我们交互性不高可以使用,但是还是不推荐。
3、阻止默认事件

4、引入fastclick.js

相关文章

微信公众号

最新文章

更多