8.点透事件.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. }
  12. #container {
  13. width: 100%;
  14. position: relative;
  15. }
  16. #under {
  17. width: 80%;
  18. height: 500px;
  19. margin: 50px auto;
  20. background-color: pink;
  21. }
  22. #dialog {
  23. width: 100%;
  24. height: 100vh;
  25. background-color: rgba(0,0,0,.3);
  26. position: fixed;
  27. top: 0;
  28. left: 0;
  29. right: 0;
  30. bottom: 0;
  31. }
  32. #tips {
  33. width: 300px;
  34. height: 300px;
  35. background-color: #fff;
  36. position: absolute;
  37. top: 100px;
  38. left: 10%;
  39. z-index: 99;
  40. }
  41. #tips #main {
  42. width: 100%;
  43. height: 180px;
  44. text-align: center;
  45. line-height: 180px;
  46. }
  47. #tips #btn {
  48. width: 100px;
  49. height: 50px;
  50. color: #fff;
  51. text-align: center;
  52. line-height: 50px;
  53. margin: 0 auto;
  54. background-color: #00f;
  55. }
  56. </style>
  57. </head>
  58. <body>
  59. <div id="container">
  60. <div id="under"></div>
  61. <div id="tips">
  62. <div id="main">弹出层</div>
  63. <div id="close">
  64. <div id="btn">关闭</div>
  65. </div>
  66. </div>
  67. <div id="dialog"></div>
  68. </div>
  69. <script>
  70. var btn = document.getElementById("btn");
  71. var dialog = document.getElementById("dialog");
  72. var under = document.getElementById("under");
  73. /**
  74. * 点透事件:
  75. * 第一个事件是touch
  76. * 第二个事件是click或者a
  77. * 两个盒子样式叠加
  78. *
  79. * 规避方法:
  80. * 1.都使用相同事件
  81. * 2.event.preventDefault()
  82. */
  83. btn.ontouchstart = function(event) {
  84. event.preventDefault()
  85. document.getElementById("tips").style.display = "none";
  86. dialog.style.display = "none";
  87. // alert("弹出")
  88. console.log("222")
  89. }
  90. under.onclick = function() {
  91. // alert("弹出")
  92. console.log("111")
  93. }
  94. // btn.ontouchstart = function() {
  95. // document.getElementById("tips").style.display = "none";
  96. // dialog.style.display = "none";
  97. // // alert("弹出")
  98. // console.log("222")
  99. // }
  100. // under.ontouchstart = function() {
  101. // // alert("弹出")
  102. // console.log("111")
  103. // }
  104. </script>
  105. </body>
  106. </html>