日期:2014-05-17  浏览次数:20678 次

CSS+JS 蒙板居中技术,居中技术,使用JS获取div的宽度高度?
<!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"  xml:lang="zh-CN" lang="zh-CN"> 
    <head> 
		<title>CSS+JS 蒙板居中技术</title> 
		
		<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> 
		<style>
			//    问题一:使用CSS+JSS实现蒙板技术

					#mb {
				position:absolute;
				background:#000;filter:alpha(opacity=30);
				z-index:99;
				margin:0 auto; 
				padding:0;
				left:0;
				top:0;
				width:100%; 
				height:100%;
			}

			#wd {
				position:absolute;
				margin:0 auto; 
				padding:0;
				top:25%;
				z-index:100;
				background:#fff;
			}
		</style>
	</head> 
	<body> 
		<div id = "mb" name = "mb" style="display:none;"></div>
		<div id ="wd"  name = "wd" style="display:none;"><img src ="1.jpg"></img></div>
		<input type="button" onclick="controlDiv('wd',1)" value="弹出遮罩层" /> 
		<iframe id="" src="http://www.baidu.com" width="100%" height="300"></iframe> 

		<script type="text/javascript"> 
		function controlDiv(DivID,divCase){

			var mengban = document.getElementById("mb");
			var divs=document.getElementById(DivID); 
		 
	          //问题二:用JS获取设置弹出div始终居中显示
                 //这个399是居中图片或者div的宽度
			var v_left=(document.body.clientWidth-399)/2 + document.body.scrollLeft; 
			var v_top=(document.body.clientHeight-divs.clientHeight)/2 + document.body.scrollTop;

			if(divCase==1){
			   divs.style.left=v_left+'px';
			   divs.style.top=v_top+'px';
			   divs.style.display="block"; 
			   mengban.style.display="block";

	         //  问题三:用JS获取DIV的宽度和高度
              	 

               //js获取div的宽度或者高度 一下三种方式都是可以的 
               //alert("divs.clientWidth--->"+divs.clientWidth);
			   //alert("divs.clientHeight--->"+divs.clientHeight);

			   //alert("divs.scrollWidth--->"+divs.scrollWidth);
			   //alert("divs.scrollHeight--->"+divs.scrollHeight);

			   //alert("divs.offsetWidth--->"+divs.offsetWidth);
			   //alert("divs.offsetHeight--->"+divs.offsetHeight);

			}else if(divCase==2){
			  divs.style.display="none";
			  mengban.style.display="none";
			}
			else
			   divs.innerHTML=divCase; //输出HTML文本到DIV
			}
		</script> 
	</body>
</html>

?