传统使用document.body.style.marginTop只能获取到内联的样式
但是如果使用css中定义的margin,则无法获取到元素实际的高度,如:
body
{ margin: 10px }
参考如下文章,可解决:
http://javascript.info/tutorial/styles-and-classes-getcomputedstyle
<script> |
06 |
if (window.getComputedStyle)
{ |
07 |
var computedStyle
= getComputedStyle(document.body, null ) |
08 |
} else { |
09 |
computedStyle
= document.body.currentStyle |
10 |
} |
11 |
alert(computedStyle.marginTop) |
12 |
</script> |