之前说的js运动不管是单物体运动,多物体运动,还是缓冲运动。在获取元素自身宽,高的时候用的都是offsetWidth,offsetHeight,但是这会引发一些麻烦!!因为在js中offsetWidth,offsetHeight获取的不仅仅是元素样式中定义的width,height还包括给元素设置的border和padding的值
看看下面的测试例子:
<style>
#div1{
width:200px;height: 100px;
background: green;
/*border: 5px solid #c00;
padding: 5px;*/
}
</style>
<body>
<div id="div1" οnclick="test();"></div>
<script>
function test() {
var oDiv = document.getElementById('div1');
alert("offsetWidth 获取的值是:"+oDiv.offsetWidth);
alert("offsetWidth 获取的值是:"+oDiv.offsetHeight);
}
</script>
</body>
不加border和padding的时候,弹出的值都是200和1