`
WessonGao
  • 浏览: 28022 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

JS弹出层居中

 
阅读更多

直接上代码:
(html 有两个div backgroundPopup和fordetail)
css美工设计 与我无关
<style type="text/css">
    	#backgroundPopup{   
		display:none;   
		position:fixed;   
		_position:absolute;   
		height:100%;   
		width:100%;   
		top:0;   
		left:0;   
		background:#000000;   
		border:1px solid #cecece;   
		z-index:1;   
		}   
		#fordetail{   
		z-index:2;   
		}   
		#popupContactClose{   
		font-size:14px;   
		line-height:14px;   
		right:6px;   
		top:4px;   
		position:absolute;   
		color:#6fa5fd;   
		font-weight:700;   
		display:block;   
		}   
    </style>


<script type="text/javascript" language="javascript">
        //初始化:是否开启DIV弹出窗口功能
        //0 表示开启; 1 表示不开启;
        var popupStatus = 0;
	function loadPopup(){   
		if(popupStatus==0){
			$("#backgroundPopup").css({   
			"opacity": "0.7"  
			});   
			$("#backgroundPopup").fadeIn("slow");   
			$("#fordetail").fadeIn("slow");  
			//获取系统变量
		windowWidth = document.documentElement.clientWidth;   
		windowHeight = document.documentElement.clientHeight;   
		popupHeight = $("#fordetail").height();   
		popupWidth = $("#fordetail").width();  
		//居中设置   
		$("#fordetail").css({   
		"position": "absolute",   
		"top": windowHeight/2-popupHeight/2+document.documentElement.scrollTop,   
		"left": windowWidth/2-popupWidth/2   
		});   
		//以下代码仅在IE6下有效
		  
		$("#backgroundPopup").css({   
		"height": windowHeight   
		}); 
			popupStatus = 1;   
		}   
	}  
	
	//使用Jquery去除弹窗效果 
	function disablePopup(){   
		//仅在开启标志popupStatus为1的情况下去除
		if(popupStatus==1){   
			$("#backgroundPopup").fadeOut("slow");   
			$("#fordetail").fadeOut("slow");   
			popupStatus = 0;   
		}   
	}  
</script>

关于获取高度和宽度的小补充(复制粘贴以防链接丢失:http://apps.hi.baidu.com/share/detail/24018719):
关于获取各种浏览器可见窗口大小的一点点研究
<script>
function getInfo()
{
var s = "";
s = " 网页可见区域宽:" document.body.clientWidth;
s = " 网页可见区域高:" document.body.clientHeight;
s = " 网页可见区域宽:" document.body.offsetWidth " (包括边线和滚动条的宽)";
s = " 网页可见区域高:" document.body.offsetHeight " (包括边线的宽)";
s = " 网页正文全文宽:" document.body.scrollWidth;
s = " 网页正文全文高:" document.body.scrollHeight;
s = " 网页被卷去的高(ff):" document.body.scrollTop;
s = " 网页被卷去的高(ie):" document.documentElement.scrollTop;
s = " 网页被卷去的左:" document.body.scrollLeft;
s = " 网页正文部分上:" window.screenTop;
s = " 网页正文部分左:" window.screenLeft;
s = " 屏幕分辨率的高:" window.screen.height;
s = " 屏幕分辨率的宽:" window.screen.width;
s = " 屏幕可用工作区高度:" window.screen.availHeight;
s = " 屏幕可用工作区宽度:" window.screen.availWidth;

s = " 你的屏幕设置是 " window.screen.colorDepth " 位彩色";
s = " 你的屏幕设置 " window.screen.deviceXDPI " 像素/英寸";
//alert (s);
}
getInfo();
</script>
在我本地测试当中:
在IE、FireFox、Opera下都可以使用
document.body.clientWidth
document.body.clientHeight
即可获得,很简单,很方便。
而在公司项目当中:
Opera仍然使用
document.body.clientWidth
document.body.clientHeight
可是IE和FireFox则使用
document.documentElement.clientWidth
document.documentElement.clientHeight
原来是W3C的标准在作怪啊
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
如果在页面中添加这行标记的话 在IE中:
document.body.clientWidth ==> BODY对象宽度
document.body.clientHeight ==> BODY对象高度
document.documentElement.clientWidth ==> 可见区域宽度
document.documentElement.clientHeight ==> 可见区域高度
在FireFox中:
document.body.clientWidth ==> BODY对象宽度
document.body.clientHeight ==> BODY对象高度
document.documentElement.clientWidth ==> 可见区域宽度
document.documentElement.clientHeight ==> 可见区域高度
?
在Opera中:
document.body.clientWidth ==> 可见区域宽度
document.body.clientHeight ==> 可见区域高度
document.documentElement.clientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽)
document.documentElement.clientHeight ==> 页面对象高度(即BODY对象高度加上Margin高)
而如果没有定义W3C的标准,则
IE为:
document.documentElement.clientWidth ==> 0
document.documentElement.clientHeight ==> 0
FireFox为:
document.documentElement.clientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽)document.documentElement.clientHeight ==> 页面对象高度(即BODY对象高度加上Margin高)
Opera为:
document.documentElement.clientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽)document.documentElement.clientHeight ==> 页面对象高度(即BODY对象高度加上Margin高)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics