Jquery实现遮罩层,就是弹出DIV周围都灰色不能操作

83540198 2014-08-01

作者:zccst

本篇文章的精髓是:

定义一个div,设置宽高跟原网页一样,通过z-index浮在原网页上面,看上去是遮罩层的效果。弹出框的z-index比div的加1.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Jquery遮罩层</title>

<style type="text/css">
body{margin:0px;} 
#BgDiv{background-color:#e3e3e3; position:absolute; z-index:99; left:0; top:0; display:none; width:100%; height:1000px;opacity:0.5;filter: alpha(opacity=50);-moz-opacity: 0.5;}

#DialogDiv{position:absolute;width:400px; left:50%; top:50%; margin-left:-200px; height:auto; z-index:100;background-color:#fff; border:1px #8FA4F5 solid; padding:1px;}
#DialogDiv h2{ height:25px; font-size:14px; background-color:#8FA4F5; position:relative; padding-left:10px; line-height:25px;}
#DialogDiv h2 a{position:absolute; right:5px; font-size:12px; color:#000000}
#DialogDiv .form{padding:10px;}

#DialogDiv2{position:absolute;width:400px; left:50%; top:50%; margin-left:-200px; height:auto; z-index:100;background-color:#fff; border:1px #8FA4F5 solid; padding:1px;}
#DialogDiv2 h2{ height:25px; font-size:14px; background-color:#8FA4F5; position:relative; padding-left:10px; line-height:25px;}
#DialogDiv2 h2 a{position:absolute; right:5px; font-size:12px; color:#000000}
#DialogDiv2 .form{padding:10px;}

#DialogDiv3{position:absolute;width:400px; left:50%; top:50%; margin-left:-200px; height:auto; z-index:100;background-color:#fff; border:1px #8FA4F5 solid; padding:1px;}
#DialogDiv3 h2{ height:25px; font-size:14px; background-color:#8FA4F5; position:relative; padding-left:10px; line-height:25px;}
#DialogDiv3 h2 a{position:absolute; right:5px; font-size:12px; color:#000000}
#DialogDiv3 .form{padding:10px;}
</style>

<script type="text/javascript" src="http://keleyi.com/keleyi/pmedia/jquery-1.9.1.min.js"></script>
<script language="javascript" type="text/javascript">
function ShowDIV(thisObjID) {
$("#BgDiv").css({ display: "block", height: $(document).height() });
var yscroll = document.documentElement.scrollTop;
$("#" + thisObjID).css("top", "100px");
$("#" + thisObjID).css("display", "block");
document.documentElement.scrollTop = 0;
}

function closeDiv(thisObjID) {
$("#BgDiv").css("display", "none");
$("#" + thisObjID).css("display", "none");
}
</script>

</head>

<body>
<div id="BgDiv"></div>

<!--遮罩层显示的DIV1-->
<div id="DialogDiv" style="display:none">
<h2>弹出层<a href="javascript:;" id="btnClose" onclick="closeDiv('DialogDiv')">关闭</a></h2>
<div class="form">我是弹出对话框111111!!</div>
</div>

<!--遮罩层显示的DIV2-->
<div id="DialogDiv2" style="display:none">
<h2>弹出层<a href="javascript:;" id="btnClose2" onclick="closeDiv('DialogDiv2')">关闭</a></h2>
<div class="form">我是弹出对话框2222!!</div>
</div>

<!--遮罩层显示的DIV3-->
<div id="DialogDiv3" style="display:none">
<h2>弹出层<a href="javascript:;" id="btnClose3" onclick="closeDiv('DialogDiv3')">关闭</a></h2>
<div class="form">我是弹出对话框3333333!!</div>
</div>

<p>
<input value="弹出遮罩层1" type="button" id="btnShow1" onclick="ShowDIV('DialogDiv')" />
<input value="弹出遮罩层2" type="button" id="btnShow2" onclick="ShowDIV('DialogDiv2')" />
<input value="弹出遮罩层3" type="button" id="btnShow3" onclick="ShowDIV('DialogDiv3')" />
</p>

<div style="background-color:#959822; width:100%;height:150px;">请滚动鼠标使页面向下</div>
<div style="background-color:Green; width:100%;height:150px;">www.keleyi.com<a href="http://keleyi.com/dev/94fb5964c80ee829.htm" target="_blank">图片返回顶部效果</a></div>
<div style="background-color:Red; width:100%;height:150px;">欢迎</div>
<div style="background-color:Yellow; width:100%;height:150px;">hi</div>
<div style="background-color:Silver; width:100%;height:150px;">柯乐义</div>
<div style="background-color:Aqua; width:100%;height:150px;">keleyi.com</div>
<div style="background-color:Fuchsia; width:100%;height:150px;">keleyi</div>
<div style="background-color:Green; width:100%;height:150px;">keleyi.com</div>
<div style="background-color:Blue; width:100%;height:150px;">柯乐义</div>
<div style="background-color:Olive; width:100%;height:150px;">柯乐义 返回顶部</div>

<div style="background-color:Green; width:100%;height:150px;">A</div>
<div style="background-color:Purple; width:100%;height:150px;">jquery</div>
<div style="background-color:Green; width:100%;height:150px;"><a href="http://keleyi.com/a/bjac/6f008786225269ac.htm" target="_blank">原文</a></div>
<div style="background-color:Lime; width:100%;height:150px;">keleyi.com</div>
<div style="background-color:Orange; width:100%;height:150px;">完整代码</div>
</body>
</html>

相关推荐