DIV始终保持在浏览器中央,兼容各浏览器版本

 

window.onresize = resetDiv;

function resetDiv(){

//浏览器宽度

var sWidth  = document.documentElement.clientWidth;

//浏览器高度

var sHeight = document.documentElement.clientHeight;

//设置对象

var obj = document.getElementById('div_login');

//设置的高度和宽度减去DIV自身的高度和宽度

var w = sWidth/2-100;

var h = sHeight/2-100;

obj.style.marginLeft=w+'px';

obj.style.marginTop=h+'px';

}

 

<body onload="resetDiv();">

你可能感兴趣的:(DIV始终居中显示)