彈窗和遮罩層的顯示隱藏及空白區域關閉
阿新 • • 發佈:2019-04-09
100% lac get .sh mouseup head nbsp show wid
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Commpatible" content="IE=edge"> <title>HTML遮罩層</title> <style> body{ background: aqua; } .shade{ position: fixed; top:0; left: 0; right: 0; width: 100%; height: 100%; background: black; opacity: 0.6; display: none; } .dialog{ display: none; position: relative; width: 200px; height: 200px; background: lightcoral; } .btn{ position: absolute; width: 50px; height: 50px; top: 40px; left: 200px; background: #c1f088; }</style> </head> <body> <div class="shade"></div> <div class="wrap"> 彈窗展示 </div> <div class="dialog"> <div class="content">內容區</div> <div class="btn">按鈕</div> </div> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.slim.js"></script> <script> //彈窗顯示 $(‘.wrap‘).click(function () { $(‘.shade‘).show() $(‘.dialog‘).show() }) // 點擊關閉按鈕,彈窗隱藏 $(‘.btn‘).click(function () { $(‘.shade‘).hide() $(‘.dialog‘).hide() }) // 點擊空白區域,彈窗隱藏 $(document).mouseup(function(e){ var _con = $(‘.dialog ‘); // 設置目標區域 // 點擊事件的對象不是目標區域本身 // 事件對象不是目標區域的子元素 if(!_con.is(e.target) && _con.has(e.target).length === 0){ // Mark 1 console.log(e) console.log(e.target) $(‘.shade‘).hide() $(‘.dialog‘).hide() } }); </script> </body> </html>
彈窗和遮罩層的顯示隱藏及空白區域關閉