| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 | <!DOCTYPE html><html lang="en"><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">  <title>Document</title>  <style>    * {      touch-action: none    }    body {      margin: 0;    }    .container {      width: 100%;      overflow: hidden;      position: relative;    }    .layer-title {      width: 100%;      margin: 50px 0;      text-align: center;    }    .layer-action {      position: absolute;      bottom: 20px;      width: 100%;      text-align: center;    }    .btn {      background-color: #08c;      border: 0;      color: #fff;      height: 30px;      line-height: 30px;      width: 100px;    }    #underLayer {      background-color: #eee;      width: 90%;      height: 500px;      line-height: 500px;      margin: 30px auto 1000px;      text-align: center;    }    #popupLayer {      /*display: none;*/      background-color: #fff;      width: 80%;      height: 200px;      position: fixed;      top: 50%;      left: 50%;      margin-left: -40%;      margin-top: -100px;      z-index: 1;    }    #bgMask {      position: fixed;      top: 0;      left: 0;      right: 0;      bottom: 0;      background-color: rgba(0, 0, 0, 0.6);    }  </style></head><body>  <div id="container">    <div id="underLayer">底层元素</div>    <div id="popupLayer">      <div class="layer-title">弹出层</div>      <div class="layer-action">        <button class="btn" id="closePopup">关闭</button>      </div>    </div>  </div>  <div id="bgMask"></div>  <script type="text/javascript">    var oClose = document.querySelector('#closePopup')    var oUnder = document.querySelector('#underLayer')    oClose.ontouchstart = function (e) {      /* 阻止事件默认行为 */      e.preventDefault()      document.querySelector('#popupLayer').style.display = 'none'      document.querySelector('#bgMask').style.display = 'none'    }    // oClose.onclick = function(){    //   document.querySelector('#popupLayer').style.display = 'none'    //   document.querySelector('#bgMask').style.display = 'none'    // }    oUnder.onclick = function () {      alert('click')    }    /*     出现点透事件的条件如下:    有两层重叠的元素    上面的元素带有touch事件  下面的元素是click事件 或者是一个a标签      事件流-> touch  -> click    上层元素点击需要触发display:none    上层元素点击的时候触发了click事件    因为click事件会有延迟时间  而touch会立即触发  所以当点击了上面的事件会触发下面的click、    解决方案:    1、把上层的事件也换成click  不会立即触发  都有延迟    2、在上层元素中 通过event.preventDefault()阻止事件默认行为    */  </script></body></html>
 |