用jQuery实现弹出窗口/弹出div层
$(function(){
var screenwidth,screenheight,mytop,getPosLeft,getPosTop
screenwidth = $(window).width();
screenheight = $(window).height();
//获取滚动条距顶部的偏移
mytop = $(document).scrollTop();
//计算弹出层的left
getPosLeft = screenwidth/2 - 260;
//计算弹出层的top
getPosTop = screenheight/2 - 150;
//css定位弹出层
$("#box").css({"left":getPosLeft,"top":getPosTop});
//当浏览器窗口大小改变时...
$(window).resize(function(){
screenwidth = $(window).width();
screenheight = $(window).height();
mytop = $(document).scrollTop();
getPosLeft = screenwidth/2 - 260;
getPosTop = screenheight/2 - 150;
$("#box").css({"left":getPosLeft,"top":getPosTop+mytop});
});
//当拉动滚动条时...
$(window).scroll(function(){
screenwidth = $(window).width();
screenheight = $(window).height();
mytop = $(document).scrollTop();
getPosLeft = screenwidth/2 - 260;
getPosTop = screenheight/2 - 150;
$("#box").css({"left":getPosLeft,"top":getPosTop+mytop});
});
//点击链接弹出窗口
$("#popup").click(function(){
$("#box").fadeIn("fast");
//获取页面文档的高度
var docheight = $(document).height();
//追加一个层,使背景变灰
$("body").append("<div id='greybackground'></div>");
$("#greybackground").css({"opacity":"0.5","height":docheight});
return false;
});
//点击关闭按钮
$("#closeBtn").click(function() {
$("#box").hide();
//删除变灰的层
$("#greybackground").remove();
return false;
});
});
源代码
<!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 pop up</title>
<script src=http://blog.soso.com/qz.q/"jquery.js" type="text/javascript"></script>
<style type="text/css">
* {
margin:0;
padding:0;
}
#wrapper {
height:1000px;
}
#box {
display:none;
position:absolute;
width:520px;
height:300px;
border:#f60 solid 2px;
z-index:200;
background:#fff;
}
#closeBtn {
position:absolute;
right:10px;
top:10px;
cursor:pointer;
}
#greybackground {
background:#000;
display:block;
z-index:100;
width:100%;
position:absolute;
top:0;
left:0;
}
</style>
</head>
<body>
<div id="wrapper">
<a href=http://blog.soso.com/qz.q/"#" id="popup">点击弹出div窗口</a>
</div>
<div id="box">
<span id="closeBtn">关闭</span>
</div>
<script type="text/javascript">
$(function(){
var screenwidth,screenheight,mytop,getPosLeft,getPosTop
screenwidth = $(window).width();
screenheight = $(window).height();
mytop = $(document).scrollTop();
getPosLeft = screenwidth/2 - 260;
getPosTop = screenheight/2 - 150;
$("#box").css({"left":getPosLeft,"top":getPosTop});
$(window).resize(function(){
screenwidth = $(window).width();
screenheight = $(window).height();
mytop = $(document).scrollTop();
getPosLeft = screenwidth/2 - 260;
getPosTop = screenheight/2 - 150;
$("#box").css({"left":getPosLeft,"top":getPosTop+mytop});
});
$(window).scroll(function(){
screenwidth = $(window).width();
screenheight = $(window).height();
mytop = $(document).scrollTop();
getPosLeft = screenwidth/2 - 260;
getPosTop = screenheight/2 - 150;
$("#box").css({"left":getPosLeft,"top":getPosTop+mytop});
});
$("#popup").click(function(){
$("#box").fadeIn("fast");
$("body").append("<div id='greybackground'></div>");
var documentheight = $(document).height();
$("#greybackground").css({"opacity":"0.5","height":documentheight});
return false;
});
$("#closeBtn").click(function() {
$("#box").hide();
$("#greybackground").remove();
return false;
});
});
</script>
</body>
</html>
JavaScript实现弹出窗口实质上就是在浏览器上画了一个方形区域,并在开始时将其隐藏,只是到某个JavaScript事件时才通过修改css的属性值来将其显示出来。
其大致步骤为:
创建一个装载弹出窗口的div view plaincopy to clipboardprint? <html> <head> <title>jQuery实例1:浮动窗口</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <mce:script type="text/javascript" src="jslib/jquery.js" mce_src="jslib/jquery.js"></mce:script> <mce:script type="text/javascript" src="jslib/jquerywin.js" mce_src="jslib/jquerywin.js"></mce:script> <link type="text/css" rel="stylesheet" href="css/win.css" mce_href="css/win.css"> </head> <body> </body> <a onclick="showWin()" href="#" mce_href="#">弹出窗口</a> <div id="win"> <div id="title">我是标题栏!<span id="close" onclick="hide()">X</span></div> <div id="content">我是一个窗口!</div> </div> </html> <html> <head> <title>jQuery实例1:浮动窗口</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <mce:script type="text/javascript" src="jslib/jquery.js" mce_src="jslib/jquery.js"></mce:script> <mce:script type="text/javascript" src="jslib/jquerywin.js" mce_src="jslib/jquerywin.js"></mce:script> <link type="text/css" rel="stylesheet" href="css/win.css" mce_href="css/win.css"> </head> <body> </body> <a onclick="showWin()" href="#" mce_href="#">弹出窗口</a> <div id="win"> <div id="title">我是标题栏!<span id="close" onclick="hide()">X</span></div> <div id="content">我是一个窗口!</div> </div> </html>
创建相应的css文件将其显示为一个弹出窗口 view plaincopy to clipboardprint? #win{ /*边框*/ border:1px red solid; /*窗口的高度和宽度*/ width : 300px; height: 200px; /*窗口的位置*/ position : absolute; top : 100px; left: 350px; /*开始时窗口不可见*/ display : none; } /*控制背景色的样式*/ #title{ background-color : blue; color : red; /*控制标题栏的左内边距*/ padding-left: 3px; } #cotent{ padding-left : 3px; padding-top : 5px; } /*控制关闭按钮的位置*/ #close{ margin-left: 188px; /*当鼠标移动到X上时,出现小手的效果*/ cursor: pointer; } #win{ /*边框*/ border:1px red solid; /*窗口的高度和宽度*/ width : 300px; height: 200px; /*窗口的位置*/ position : absolute; top : 100px; left: 350px; /*开始时窗口不可见*/ display : none; } /*控制背景色的样式*/ #title{ background-color : blue; color : red; /*控制标题栏的左内边距*/ padding-left: 3px; } #cotent{ padding-left : 3px; padding-top : 5px; } /*控制关闭按钮的位置*/ #close{ margin-left: 188px; /*当鼠标移动到X上时,出现小手的效果*/ cursor: pointer; }
创建相应的JavaScript文件来操作窗口的显示 view plaincopy to clipboardprint? function showWin(){ /*找到div节点并返回*/ var winNode = $("#win"); //方法一:利用js修改css的值,实现显示效果 // winNode.css("display", "block"); //方法二:利用jquery的show方法,实现显示效果 // winNode.show("slow"); //方法三:利用jquery的fadeIn方法实现淡入 winNode.fadeIn("slow"); } function hide(){ var winNode = $("#win"); //方法一:修改css的值 //winNode.css("display", "none"); //方法二:jquery的fadeOut方式 winNode.fadeOut("slow"); //方法三:jquery的hide方法 winNode.hide("slow"); }