层 js控制垂直 水平居中

<head> 转载请注明 http://netsos.cnblogs.com/
<script type="text/javascript" defer="defer">
function aa(w,h,ob){
    
var obj=document.getElementById(ob);
    alert(obj);
    
//设置宽高
    obj.style.width=w+"px";
    
    alert(obj.style.width);
    
    obj.style.height
=h+"px";
    
    alert(obj.style.height);
    
    
//获取宽高
    var wid=obj.clientWidth;
    
    
var hei=obj.clientHeight;
    
    
    obj.style.marginLeft
=-wid/2+"px";
    obj.style.marginTop=-hei/2+"px";
    
}
aa(
300,200,"d1");
</script>
 转载请注明 http://netsos.cnblogs.com/
<title>层垂直居中</title>
</head>
<body >

 转载请注明 http://netsos.cnblogs.com/
<div id="d1" style="background-color:#33C; z-index:12;position:absolute; top:50%; left:50%;"></div>
</body>

 

posted @ 2010-06-08 18:36  hevily  Views(2087)  Comments(1Edit  收藏  举报