HTML:
<div id="div">点击除开div的区域可以弹出弹窗</div>
<div id="cover"></div>
<div id="box">点击除开div和弹窗的区域可以关闭弹窗</div>
CSS:
#div{
position:relative;
z-index:2;
width:300px;
height:200px;
border:1px solid grey;
}
#cover{
position:fixed;
width:100%;
height:100%;
left:0;
top:0;
z-index:1;
}
#box{
border:1px solid grey;
position:fixed;
width:400px;
height:300px;
left:50%;
top:50%;
margin:-150px 0 0 -200px;
z-index:2;
display:none;
}
JavaScript:
document.getElementById("cover").onclick = function() {
if (document.getElementById("box").style.display == "block") {
document.getElementById("box").style.display = "none";
document.getElementById("cover").style.background = "white";
}
else {
document.getElementById("box").style.display = "block";
document.getElementById("cover").style.background = "#aaa";
}
}