日期:2014-05-17 浏览次数:20767 次
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
#wraps{
margin:0px auto; width:960px;
border:1px solid red;
position:relative; left:0; top:0;
}
</style>
</head>
<body>
<div id="wraps">123</div>
<button id="btn">body长高了,div会移动吗?</button>
<script>
var $ = function(id){
return document.getElementById(id);
};
var $t = function(tag, cot){
cot = cot || document;
return cot.getElementsByTagName(tag);
};
var a = document.body.offsetWidth;
$('btn').onclick = function(){
$t('body')[0].style.height = '2000px';
var b = document.body.offsetWidth;
var t = a - b;
//console.log(t)
if( t ){
$('wraps').style.left = t/2+'px';
}
}
</script>
</body>
</html>
------解决方案--------------------
5楼的,如果切换浏览器窗口大小呢?最大化,还原……