8.点透事件.html 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #box {
  13. width: 100vw;
  14. position: relative;
  15. }
  16. #under {
  17. width: 90%;
  18. height: 500px;
  19. background: rgba(0, 0, 0, .1);
  20. margin: 10px auto;
  21. text-align: center;
  22. line-height: 500px;
  23. font-size: 25px;
  24. }
  25. #vase {
  26. width: 80%;
  27. height: 300px;
  28. background: #fff;
  29. position: absolute;
  30. top: 100px;
  31. left: 37px;
  32. z-index: 9;
  33. }
  34. #close {
  35. width: 100px;
  36. height: 30px;
  37. text-align: center;
  38. line-height: 30px;
  39. background: #00f;
  40. color: #fff;
  41. position: absolute;
  42. top: 50%;
  43. left: 50%;
  44. margin-top: -15px;
  45. margin-left: -50px;
  46. }
  47. #dialog {
  48. width: 100%;
  49. height: 100vh;
  50. position: fixed;
  51. top: 0;
  52. left: 0;
  53. background: rgba(0,0,0,.5);
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <div id="box">
  59. <div id="under">底层元素</div>
  60. <div id="vase">
  61. <div id="tit">弹出层</div>
  62. <div id="close">关闭</div>
  63. </div>
  64. <div id="dialog"></div>
  65. </div>
  66. <script>
  67. /**
  68. * 点透事件:
  69. * 1.两层元素叠加
  70. * 2.第一层使用touch事件
  71. * 3.第二层使用click事件/a标签
  72. */
  73. var close = document.getElementById("close");
  74. var under = document.getElementById("under");
  75. close.ontouchstart = function(event) {
  76. event.preventDefault();
  77. document.getElementById("vase").style.display = 'none';
  78. document.getElementById("dialog").style.display = 'none';
  79. }
  80. under.onclick = function() {
  81. alert("弹出")
  82. }
  83. </script>
  84. </body>
  85. </html>