Js弹出窗口在任意分辨率下居中显示的实例代码
本节内容:
弹出窗口在任意分辨率下居中显示
本节提供的这段代码,分为三个部分:html页面、js代码、css样式表。
希望此段代码,有助于大家掌握js检测分辨率,以实现弹出窗口居中显示的方法。
1,html代码部分
复制代码 代码示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>弹出窗口_jquerycn.cn</title>
<link type="text/css" rel="stylesheet" href=http://www.jquerycn.cn/a_10681/"window.css">
<script language="javascript" type="text/javascript" src=http://www.jquerycn.cn/a_10681/"../../jquery/jquery.js"></script>
<script language="javascript" type="text/javascript" src=http://www.jquerycn.cn/a_10681/"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=http://www.jquerycn.cn/a_10681/"close.gif">csdn欢迎您</div>
<div class="content">jquery中文网,专业的网站技术站点。</div>
</div>
<div class="window" id="right">
<div class="title"><img src=http://www.jquerycn.cn/a_10681/"close.gif">csdn欢迎您</div>
<div class="content">jquery中文网,专业的网站技术站点。</div>
</div>
<div class="window" id="left">
<div class="title"><img src=http://www.jquerycn.cn/a_10681/"close.gif">csdn欢迎您</div>
<div class="content">jquery中文网,专业的网站技术站点。</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;
}
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://www.juheyunku.com/jiaob/shell/10462.shtml
相关文章
热门TAG
命令 权重 外链 企业网站 白帽 php 织梦教程 dedecms修改内容 javascript 织梦 功能 标签 调用 详解 服务器 网站流量 实例解析 Dedecms 织梦cms HTML tags标签 python jquery教程 jquery windows SEO优化 蜘蛛 搜索引擎 网站收录 JSP最新文章
-
Centos下PHP5升级为PHP7的方法
时间:2021-01-03
-
在php当中常量和变量的区
时间:2020-12-29
-
PHP中经典的四大排序算法
时间:2020-12-29
-
dw怎么运行php文件?
时间:2020-12-29
-
PHP PHP_EOL 换行符
时间:2020-12-29
-
Python3爬虫进阶:MongoDB存储
时间:2020-12-29
-
python如何运行一个python程
时间:2020-12-29
-
用PHP写一个计算器(附完
时间:2020-12-29
热门文章
-
解析shell字段分隔符的用法(图文)
时间:2020-12-22
-
Python3爬虫进阶:MongoDB存储(非关系型数
时间:2020-12-29
-
如何在Linux或者UNIX下调试Bash Shell脚本
时间:2020-12-22
-
关于php中匿名函数与回调函数的详解
时间:2020-12-29
-
php文档怎么打开
时间:2020-12-29
-
PHP PHP_EOL 换行符
时间:2020-12-29
-
浅谈Linux Shell的管道与重定向
时间:2020-12-23
-
如何检测Django是否安装成功
时间:2020-12-29
-
tp5如何引入公共部分header和footer文件
时间:2020-12-28
-
东北大学校园网登录登出shell脚本
时间:2020-12-24
