关于弹出div层居中的问题及解决方法
来源:互联网 发布:虹港数据 idc机房 编辑:程序博客网 时间:2024/05/29 18:47
弹出div层在做网站中用到的也不少,但是如何尽可能的做到兼容各个浏览器的各个版本,这可是个大问题了,相信这也是大家头疼的问题。同一个方法在不同的浏览器得到的值是不同的,就拿document.documentElement.scrollTop来说吧。
ie下document.documentElement.scrollTop是不为0的;
chrome下document.documentElement.scrollTop却为0;
同时,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">
或者
<!DOCTYPE html>
<html>
总之类似的问题多了去了。
下面给出一个相对比较兼容的div弹出层的解决方案:
<!DOCTYPE html><html><head runat="server"> <title>sssssss</title> <script src="Scripts/jquery-1.4.4.min.js" type="text/javascript"></script> <style> body { height: 5000px; } .top-1 { height: 1000px; border: solid 1px red; } .top-1 { height: 500px; border: solid 1px red; } .box { position: fixed; left: 50%; top: 50%; display: none; border: 1px solid blue; width: 500px; height: 300px; } </style></head><body> <div id="box" class="box"> ssssssssssss</div> <div class="top-1"> </div> <input type="button" id="show_1" value="click" onclick="showBox();" /> <div class="top-2"></div> <input type="button" id="show_2" value="click" /> <script type="text/javascript"> //判断是否是ie6,如果是ie6则设置box的position属性为absolute,并添加onscroll事件; //否则设置box的position属性为fixed //首先要设置box样式position: fixed; left: 50%; top: 50%; function showBox() { var box = $("#box"); box.show(); if ($.browser.msie && $.browser.version == "6.0") { setIe6Box(); window.onscroll = function () { setIe6Box(); } function setIe6Box() { var box_margin_top = document.documentElement.scrollTop - box.height() / 2 + "px"; var box_margin_left = document.documentElement.scrollLeft - box.width() / 2 + "px"; box.css({ "position": "absolute", "margin-top": box_margin_top, "margin-left": box_margin_left }); } } else { box.css({ "margin-top": (-box.height() / 2 + "px"), "margin-left": (-box.width() / 2 + "px") }); } } </script></body></html>基于中国用户大多数使用的是ie,这个解决方案也基本上够用了。
- 关于弹出div层居中的问题及解决方法
- 弹出居中的div层
- layer 弹出层不居中显示问题的解决方法
- DIV始终居中的半透明弹出层
- 关于Div弹出层无法取值的解决方法
- 一个关于里层div在外层div里居中的问题
- Div+CSS实现始终居中的半透明弹出层
- 关于子DIV居中的问题
- 弹出层Div 覆盖不住select控件的解决方法
- 嵌入的flash遮挡弹出div层解决方法
- JS弹出居中的DIV
- 让弹出的div居中
- JS弹出居中的DIV
- 关于div内嵌div居中的问题
- 关于弹出层定位的问题
- 关于DIV高度居中问题
- div水平垂直居中问题以及解决方法的兼容性
- html DIV始终垂直居中的半透明弹出层特效源代码下载
- flex4+spring整合有关问题,Unexpected child element 'factory' found in 'destination' from
- 怎样实现点击button查询从数据库中找出符合条件的结果。。。求问(以下代码可以否)
- Hubbub: 号称第一的语音社交网站上线
- Android 常用代码收集
- 远程调用 比较 jboss server
- 关于弹出div层居中的问题及解决方法
- “此致敬礼”是什么意思
- POJ 1989 The Cow Lineup [dp]
- Struts2简单配置国际化资源文件及用法
- SELinux false alarm for some utilities
- 利用CodeIgniter中的Email类发邮件
- 果粉福音: 金士顿发布Wi-Drive扩展IOS设备存储
- 构建利器Lisp的创作环境
- 32位装64位