| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- * {
- margin: 0;
- padding: 0;
- list-style: none;
- text-decoration: none;
- box-sizing: border-box;
- }
- #container {
- width: 100vw;
- position: relative;
- }
- #under {
- width: 90%;
- height: 520px;
- color: #fff;
- font-size: 28px;
- text-align: center;
- line-height: 520px;
- background: #ccc;
- margin: 0 auto;
- }
- #dialog {
- width: 80%;
- height: 300px;
- background: #fff;
- position: absolute;
- top: 20%;
- left: 40px;
- z-index: 99;
- }
- #title {
- font-size: 20px;
- width: 100%;
- height: 200px;
- text-align: center;
- line-height: 200px;
- }
- #btn {
- width: 100%;
- height: 100px;
- }
- #close {
- width: 100px;
- height: 40px;
- text-align: center;
- line-height: 40px;
- color: #fff;
- background: #00f;
- margin: 0 auto;
- }
- #mask {
- width: 100%;
- height: 100vh;
- position: absolute;
- top: 0;
- left: 0;
- background:rgba(0,0,0,.3)
- }
- </style>
- </head>
- <body>
- <div id="container">
- <div id="under">底层元素</div>
- <div id="dialog">
- <div id="title">弹出层</div>
- <div id="btn">
- <div id="close">关闭</div>
- </div>
- </div>
- <div id="mask"></div>
- </div>
- <script>
- /**
- * 点透事件
- * 1.两层元素叠加到一起
- * 2.第一层是touch事件
- * 3.第二层是click事件或者a标签
- *
- *
- * 解决方案:
- * 1.event.preventDefault()
- * 2.将click事件全部换成touch
- * */
- let close = document.getElementById("close");
- let under = document.getElementById("under");
- close.ontouchstart = function(event) {
- event.preventDefault();
- document.getElementById("dialog").style.display = 'none';
- document.getElementById("mask").style.display = 'none';
- }
- under.onclick = function() {
- alert("弹出")
- }
- </script>
- </body>
- </html>
|