主要是理清scrollHeight、clientHeight、offsetHeight、scrollTop、offsetTop,先前一直混淆不清。
css
html: body{ height:2700px; } div{ height:2600px; width:2400px; background: tan; } html: <div> </div>
显示效果
在chrome里面看html与body两个的属性测量。
body结果以下
html结果以下
html
document.body.style.height
。它是比较特殊的一个值,由于它属性对象的style对象的一个成员,它的值是一个String,而其它的值都是Int类型的。不过这种调用方式只能获得行内样式。要想获得真实的height属性,就要使用getComputedStyle(document.body).height了。IE是使用currentSytle,因此要作个选择操做。document.body.clientHeight
为2700px,而若是设置body的css为{padding:0,margin:0}的话,则会获得2716。缘由是body的margin为8px。而后就某一个设置了overflow:scroll的div元素来测试。chrome
css: div#d{ width:160px; height:160px; border: solid black 30px; padding:30px; margin:30px; overflow: scroll; } html: <div id='d'>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati asperiores, saepe rerum maiores id iste doloremque quia. Veniam praesentium odio excepturi ducimus quisquam vel cumque harum adipisci tempore, fuga, aut?</div>
显示的结果以下浏览器
而后在chrome下看3个height的值测试
而在FF下分别是19六、280、324。
而后发现padding对三个height值全是有影响的(注释了padding三个值全减了60,但在FF下scrollHeight只减了30左右,变成293,很其怪,不过chrome根据个人测试chrome的结果始终是合理的),而margin全无效。
而后分别注释其余语句,最终的结论是:ui
两个top的值也是挻好理解的。code
offsetParent
是当前元素最近的采用了非静止定位的祖先元素。若是不存在这样的祖先元素,那么offsetParent就是body。offsetParent
是一个很重要的概念!!!要想明确各类浮动和定位等的关系就要准确的找出各个元素的非静止定位的祖先元素。很气的就是,虽然看到许多文章都写了这些内容,但好多错的啊!!而后只能本身动手了。htm