$(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"); }