首页 > 代码库 > 让块级元素水平垂直居中

让块级元素水平垂直居中

			function fun(){
                   var wWidth = null,
                     wHeight = null; if(window.innerHeight && window.innerWidth){ wHeight = window.innerHeight;
                        wWidth = window.innerWidth; }else if((document.body) && (document.body.clientHeight) && (document.body.clientWidth)){
                        wWidth = window.clientWidth; wHeight = window.clientHeight; }
                   var boxWidth = document.getElementById("box").clientWidth; var boxHeight = document.getElementById("box").clientHeight;
                   box.style.marginLeft = (wWidth - boxWidth)/2 + "px"; box.style.marginTop = (wHeight - boxHeight)/2 + "px"; }
              window.onload = window.onresize = function(){fun()}

让块级元素水平垂直居中