8_点透事件.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. * {
  10. touch-action: none
  11. }
  12. body {
  13. margin: 0;
  14. }
  15. .container {
  16. width: 100%;
  17. overflow: hidden;
  18. position: relative;
  19. }
  20. .layer-title {
  21. width: 100%;
  22. margin: 50px 0;
  23. text-align: center;
  24. }
  25. .layer-action {
  26. position: absolute;
  27. bottom: 20px;
  28. width: 100%;
  29. text-align: center;
  30. }
  31. .btn {
  32. background-color: #08c;
  33. border: 0;
  34. color: #fff;
  35. height: 30px;
  36. line-height: 30px;
  37. width: 100px;
  38. }
  39. #underLayer {
  40. background-color: #eee;
  41. width: 90%;
  42. height: 500px;
  43. line-height: 500px;
  44. margin: 30px auto 1000px;
  45. text-align: center;
  46. }
  47. #popupLayer {
  48. /*display: none;*/
  49. background-color: #fff;
  50. width: 80%;
  51. height: 200px;
  52. position: fixed;
  53. top: 50%;
  54. left: 50%;
  55. margin-left: -40%;
  56. margin-top: -100px;
  57. z-index: 1;
  58. }
  59. #bgMask {
  60. position: fixed;
  61. top: 0;
  62. left: 0;
  63. right: 0;
  64. bottom: 0;
  65. background-color: rgba(0, 0, 0, 0.6);
  66. }
  67. </style>
  68. </head>
  69. <body>
  70. <div id="container">
  71. <div id="underLayer">底层元素</div>
  72. <div id="popupLayer">
  73. <div class="layer-title">弹出层</div>
  74. <div class="layer-action">
  75. <button class="btn" id="closePopup">关闭</button>
  76. </div>
  77. </div>
  78. </div>
  79. <div id="bgMask"></div>
  80. <script type="text/javascript">
  81. var oClose = document.querySelector('#closePopup')
  82. var oUnder = document.querySelector('#underLayer')
  83. oClose.ontouchstart = function (e) {
  84. /* 阻止事件默认行为 */
  85. e.preventDefault()
  86. document.querySelector('#popupLayer').style.display = 'none'
  87. document.querySelector('#bgMask').style.display = 'none'
  88. }
  89. // oClose.onclick = function(){
  90. // document.querySelector('#popupLayer').style.display = 'none'
  91. // document.querySelector('#bgMask').style.display = 'none'
  92. // }
  93. oUnder.onclick = function () {
  94. alert('click')
  95. }
  96. /*
  97. 出现点透事件的条件如下:
  98. 有两层重叠的元素
  99. 上面的元素带有touch事件 下面的元素是click事件 或者是一个a标签
  100. 事件流-> touch -> click
  101. 上层元素点击需要触发display:none
  102. 上层元素点击的时候触发了click事件
  103. 因为click事件会有延迟时间 而touch会立即触发 所以当点击了上面的事件会触发下面的click、
  104. 解决方案:
  105. 1、把上层的事件也换成click 不会立即触发 都有延迟
  106. 2、在上层元素中 通过event.preventDefault()阻止事件默认行为
  107. */
  108. </script>
  109. </body>
  110. </html>