您的位置:首页 - 教程 - jQuery - 正文
jquery遮罩层简单实现
弹出遮罩层

或者到这里体验效果:http://hovertree.com/texiao/jquery/11.htm

以前会用到
var screenWidth = $(document).width();//当前窗口宽度
var screenHeight = $(document).height();//当前窗口高度
或者
var screenWidth = $(window).width();//当前窗口宽度
var screenHeight = $(window).height();//当前窗口高度

以前的代码:
<div style=" display:none" id="zzhovertree"></div>
<script type="text/javascript">
function showUnitInfo()
{
var screenWidth = $(document).width();//当前窗口宽度
var screenHeight = $(document).height();//当前窗口高度

$("#zzhovertree").css({"display":"","position": "fixed","background": "#000","z-index": "1001","-moz-opacity": "0.5","opacity":".50","filter": "alpha(opacity=50)","width":screenWidth,"height":screenHeight});
}
</script>
<a href="javascript:showUnitInfo()">弹出遮罩层</a>



现在不用了,现在的代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>jquery遮罩层简单实现 - 何问起</title>
<style>body{margin:0px}</style>
<meta charset="utf-8" />
<script type="text/javascript" src="http://hovertree.com/ziyuan/jquery/jquery-1.11.3.min.js"></script>
</head>
<body>
<div style="display:none" id="zzhovertree"></div>
<script type="text/javascript">
function ZZhovertree()
{
$("#zzhovertree").css({"display":"","position": "fixed","left":"0px","top":"0px","background": "#000","z-index": "1001","-moz-opacity": "0.5","opacity":".50","filter": "alpha(opacity=50)","width":"100%","height":"100%"});
}
$("#zzhovertree").on("click", function () { $("#zzhovertree").slideUp()})
</script>
<div style="width:768px;margin:50px auto;">
<a href="javascript:ZZhovertree();">弹出遮罩层</a>
<a href="http://hovertree.com/hvtart/bjae/o5so5i9x.htm">原文</a>
<a href="http://hovertree.com">首页</a> <a href="http://hovertree.com/texiao/">特效</a>
</div>
<div style="background-color:#959822; width:100%;height:150px;">请点击本页面的 弹出遮罩层 链接体验效果</div>
<div style="background-color:Green; width:100%;height:150px;">
hovertree.com
</div>
<div style="width:738px;height:186px;margin:10px auto;">
</div>
<div style="background-color:Red; width:100%;height:150px;">欢迎</div>
<div style="background-color:Yellow; width:100%;height:150px;"><a href="http://hovertree.com/">首页</a></div>
<div style="background-color:Silver; width:100%;height:150px;">何问起</div>
<div style="background-color:Aqua; width:1336px;height:150px;">hovertree.com</div>
<div style="background-color:Fuchsia; width:100%;height:150px;"><a href="http://hovertree.com/texiao/">更多特效</a></div>
<div style="background-color:Green; width:100%;height:150px;">hovertreetofoot</div>
<div style="background-color:Blue; width:100%;height:150px;">柯乐义</div>
<div style="background-color:Olive; width:100%;height:350px;">何问起 返回顶部</div>
<div style="background-color:Green; width:100%;height:150px;">HoverTree</div>
<div style="background-color:Purple; width:100%;height:150px;">jquery</div>
<div style="background-color:Green; width:100%;height:150px;"><a href="http://hovertree.com/hvtart/bjae/65358656.htm" target="_blank">原文</a></div>
<div style="background-color:Lime; width:100%;height:150px;">hovertree.com</div>
<div style="background-color:Orange; width:100%;height:150px;">完整代码</div>
<div style="height:130px;"></div>
</body>
</html>

jquery写的,使用前需要引用类库。

先写一个不显示的div,当触发事件时,为这个div添加样式,高度=当前窗口高度,宽度=当前窗口宽度。


改进代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>jquery遮罩层简单实现 - 何问起</title>
<style>body{margin:0px}a{color:blue}</style>
<meta charset="utf-8" />
<script type="text/javascript" src="http://hovertree.com/ziyuan/jquery/jquery-1.11.3.min.js"></script>
</head>
<body>
<div style="background-color:#959822; width:100%;height:150px;">
请点击本页面的 弹出遮罩层 链接体验效果</div>

<div style="width:768px;margin:50px auto;">
<a href="javascript:ZZhovertree();">弹出遮罩层</a>
<a href="http://hovertree.com/hvtart/bjae/o5so5i9x.htm">原文</a>
<a href="http://hovertree.com">首页</a> <a href="http://hovertree.com/texiao/">特效</a> <a href="http://hovertree.com/menu/jquery/">jQuery</a>
<br />双击退出遮罩层
</div>
<div style="width:738px;height:186px;margin:10px auto;">
</div>
<div style="background-color:Green; width:100%;height:150px;"><h3>jquery弹出遮罩层</h3>
hovertree.com
</div>
<div style="background-color:Red; width:100%;height:150px;">欢迎</div>
<div style="background-color:Yellow; width:100%;height:150px;"><a href="http://hovertree.com/">首页</a></div>
<div style="background-color:Silver; width:100%;height:150px;">何问起</div>
<div style="background-color:Aqua; width:1336px;height:150px;">hovertree.com</div>
<div style="background-color:Fuchsia; width:100%;height:150px;"><a href="http://hovertree.com/texiao/">更多特效</a></div>
<div style="background-color:Green; width:100%;height:150px;">hovertreetofoot</div>
<div style="background-color:Blue; width:100%;height:150px;">柯乐义</div>
<div style="background-color:Olive; width:100%;height:350px;">何问起 返回顶部</div>
<div style="background-color:Green; width:100%;height:150px;">HoverTree</div>
<div style="background-color:Purple; width:100%;height:150px;">jquery</div>
<div style="background-color:Green; width:100%;height:150px;"><a href="http://hovertree.com/hvtart/bjae/65358656.htm" target="_blank">原文</a></div>
<div style="background-color:Lime; width:100%;height:150px;">hovertree.com</div>
<div style="background-color:Orange; width:100%;height:150px;">完整代码</div>
<div style="height:130px;"></div>
<script type="text/javascript">
function ZZhovertree()
{
if ($("#zzhovertree").length==0) {
$('<div id="zzhovertree"></div>').appendTo($("body"));
$('#zzhovertree').on("dblclick", function () { $("#zzhovertree").hide() })
}
$('#zzhovertree').css({ "display": "", "position": "fixed", "left": "0px", "top": "0px", "background": "#000", "z-index": "1001", "-moz-opacity": "0.5", "opacity": ".50", "filter": "alpha(opacity=50)", "width": "100%", "height": "100%" }).show();
}
</script>

</body>
</html>

评论: