Js控制弹窗实现在任意分辨率下居中显示
2013-08-02 06:56
507 查看
弹窗居中比较烦人的是怎么才能在任意分辨率下实现居中显示。
1,html部分
2,js代码部分
3,CSS代码部分
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.jbxue.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代码部分
<script> //窗口的高度 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(); } </script>
3,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; }
相关文章推荐
- Js控制弹窗实现在任意分辨率下居中显示
- Js控制弹窗实现在任意分辨率下居中显示
- Js控制弹窗实现在任意分辨率下居中显示
- Js控制弹窗在任意分辨率居中
- 纯js代码实现未知宽高的元素在指定元素中垂直水平居中显示
- JS 实现div 居中显示
- JS实现让滚轮控制网页头部显示与隐藏
- JS实现始终居中显示的对话框
- JS实现用下拉框控制文本框的显示与隐藏?
- js实现弹窗居中的简单实例
- JS实现显示带倒影的图片横排居中放大展示特效实例【测试可用】
- js实现弹窗居中
- jQuery插件实现控制网页元素动态居中显示
- 用js控制实现逐字显示
- 微信小程序弹出loading层的两种方法:直接在代码里控制,在wxml文件里布局弹窗loading层,利用条件渲染,在js代码里控制是否显示loading层。
- js实现鼠标的移入移出控制图片的明暗显示
- js 弹窗控制,实现可拖拽,设置大小
- JS实现图片垂直居中显示小结
- JS实现div居中显示的例子
- JS实现控制图片显示大小的方法【图片等比例缩放功能】