教程集 www.jiaochengji.com
教程集 >  脚本编程  >  javascript  >  正文 Js弹出窗口在任意分辨率下居中显示的实例代码

Js弹出窗口在任意分辨率下居中显示的实例代码

发布时间:2015-03-08   编辑:jiaochengji.com
分享一例js代码,实现弹出窗口在任意分辨率的屏幕中,都可以居中显示的效果,感兴趣的朋友可以参考研究下。

本节内容:
弹出窗口在任意分辨率下居中显示

本节提供的这段代码,分为三个部分:html页面、js代码、css样式表。

希望此段代码,有助于大家掌握js检测分辨率,以实现弹出窗口居中显示的方法。

1,html代码部分
 

复制代码 代码示例:
<!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>弹出窗口_www.jiaochengji.com</title>
<link type="text/css" rel="stylesheet" href="window.css">
<script language="javascript" type="text/javascript" src="../../jquery/jquery.js"></script>
<script language="javascript" type="text/javascript" src="window.js"></script>
<script language="javascript">
$(document).ready(function (){
$("#btn_center").click(function (){
$(window).scroll(function (){
popcenterWindow();
});
});
$("#btn_right").click(function (){
$(window).scroll(function (){
poprightWindow();
});
});
$("#btn_left").click(function (){
$(window).scroll(function (){
popleftWindow();
});
});
});
</script>
</head>
<body>
<input type="button" value="弹出居中的窗口" id="btn_center">
<input type="button" value="弹出居右的窗口" id="btn_right">
<input type="button" value="弹出居左的窗口" id="btn_left">
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div class="window" id="center">
<div class="title"><img src="close.gif">csdn欢迎您</div>
<div class="content">教程集,专业的网站技术站点。</div>
</div>
<div class="window" id="right">
<div class="title"><img src="close.gif">csdn欢迎您</div>
<div class="content">教程集,专业的网站技术站点。</div>
</div>
<div class="window" id="left">
<div class="title"><img src="close.gif">csdn欢迎您</div>
<div class="content">教程集,专业的网站技术站点。</div>
</div>
</body>
</html>

2,js代码部分
 

复制代码 代码示例:

//窗口的高度
var windowHeight;
//窗口的宽度
var windowWidth;
//弹窗的高度
var popHeight;
//弹窗的宽度
var popWidth;
//滚动条滚动的高度
var scrollTop;
//滚动条滚动的宽度
var scrollleft;
//延时的时间
var timeout;
function init(){
//获得窗口的高度
windowHeight=$(window).height();
//获得窗口的宽度
windowWidth=$(window).width();
//获得弹窗的高度
popHeight=$(".window").height();
//获得弹窗的宽度
popWidht=$(".window").width();
//获得滚动条的高度
scrollTop=$(window).scrollTop();
//获得滚动条的宽度
scrollleft=$(window).scrollLeft();
}
//定义关闭窗口
function closeWindow(){
$(".title img").click(function (){
$(this).parent().parent().hide("slow");

});

}
//定义弹出窗口的方法
function popcenterWindow(){
//先清空上一次的延迟
clearTimeout(timeout);
timeout=setTimeout(function (){
init();
var popY=(windowHeight-popHeight)/2+scrollTop;
var popX=(windowWidth-popWidht)/2+scrollleft;
$("#center").animate({top:popY,left:popX},300).show("slow");},300);
closeWindow();
}
function popleftWindow(){
clearTimeout(timeout);
timeout=setTimeout(function (){
init();
var popY=windowHeight+scrollTop-popHeight-10;
var popX=scrollleft-5;
$("#left").animate({top:popY,left:popX},300).show("slow");},300);
closeWindow();
}
function poprightWindow(){
clearTimeout(timeout);
timeout=setTimeout(function (){
init();
var popY=windowHeight-popHeight+scrollTop-10;
var popX=windowWidth-popWidht+scrollleft-10;
$("#right").animate({top:popY,left:popX},300).show("slow");},300);
closeWindow();
}

css代码部分:
 

复制代码 代码示例:

.window{
width:250px;
background-color:#3FF;
padding:2px;
margin:5px;
position:absolute;
display:none;
}
.content{
height:150px;
background-color:#FFF;
padding:2px;
font-size:14px;
overflow:auto;
}

.title{
padding:2px;
color:#999;
font-size:14px;
}
.title img{
float:right;
cursor:pointer;
}
 

>>>查看更多javascript 教程

您可能感兴趣的文章:
Js弹出窗口在任意分辨率下居中显示的实例代码
js获取电脑分辨率的简单例子
解析jquery获取父窗口的元素
jQuery弹出窗口完整代码(居中,居左,居右)
jQuery获取浏览器中的分辨率实现代码
javascript弹出窗口实例讲解(多个例子,多种实现方法)
JS父子窗口相互取值与赋值的方法参考
js检测屏幕分辨率的例子
js获取屏幕宽度实例代码
JS获取屏幕、浏览器窗口大小、网页高度宽度的方法详解

关键词: 弹出窗口   
[关闭]
~ ~