123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <!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>
- 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: aqua;
- border: 0;
- color: white;
- height: 30px;
- width: 100px;
- line-height: 30px;
- }
- #underLayer{
- background-color: #eee;
- width: 90%;
- height: 500px;
- line-height: 500px;
- margin: 30px auto 1000px;
- text-align: center;
- }
- #popupLayer{
- background-color: #fff;
- width: 80%;
- height: 200px;
- position: fixed;
- top: 50%;
- left: 50%;
- margin-left: -40%;
- margin-top: -100px;
- z-index: 1;
- }
- #mask{
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background-color: rgba(0, 0, 0, 0.5);
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div id="underLayer"> 底层元素 </div>
- <div id="popupLayer">
- <div class="layer-title">弹出层</div>
- <div class="layer-action">
- <button class="btn" id="close"> 关闭</button>
- </div>
- </div>
- </div>
- <div id="mask"></div>
- <script>
- var oClose = document.querySelector('#close')
- var oUnder = document.querySelector('#underLayer')
- oClose.ontouchstart = function(e){
- //取消事件默认行为
- e.preventDefault()
- //框隐藏
- document.querySelector('#popupLayer').style.display = 'none'
- //遮罩层
- document.querySelector('#mask').style.display = 'none'
- }
- // oClose.onclick = function(){
- // document.querySelector('#popupLayer').style.display = 'none'
- // document.querySelector('#mask').style.display = 'none'
- // }
- oUnder.onclick = function(){
- alert('click')
- }
- /*
- 解决办法:
- 1、把上面的事件也换成click事件 这样就不会立即触发 都有延迟
- 2、在上层元素的事件中 通过event.preventDefault()取消事件的默认行为
- */
- /*
- 出现点透事件 点击穿透问题
- A层覆盖在B层上面,在A层触发touch事件后A层隐藏,会触发B层的click事件
- 用户在触摸屏幕的时候 系统会同时产生click和touch事件
- 并且 事件流是 touchstart->touchmove->touchend->click
- 当用户触摸屏幕时,A层隐藏,300ms后出发了click 但是A层已经没有了
- 因此click就落在了B层上面 触发了B层的click事件
- */
- </script>
- </body>
- </html>
|