花点时间搞清top、postop、scrolltop、scrollHeight、offsetHeight收藏 此属性仅仅在对象的定位(position)属性被设置时可用。否则,此属性设置会被忽略。 <div style="background-color:red; position:absolute; width:100px; height:100px;"> <p style="background-color:silver; position:absolute; top:-5px;">测试top</p> </div> 需要注意的是,DIV和P这一对包含元素,都需要设置position为absolute才能得到想要的结果,假如父元素不设置,则子元素的参照将是更上层定义过position的元素,直到整个文档; 2. posTop posTop的数值其实和top是一样的,但区别在于,top固定了元素单位为px,而posTop只是一个数值(这一点可以通过alert("top="+id.style.top)和alert("posTop="+id.style.posTop)来证明),因此一般使用posTop来进行运算。 <div style="background-color:red; position:absolute; width:100px; height:100px;"> <p id="test" style="background-color:silver; position:absolute;">测试posTop</p> </div> <script> 3. scrollTop <div id="container" style="background-color:silver; width:100px; height:100px; overflow:auto;"> <script> 注意设置方式是id.scrollTop,而不是id.style.scrollTop。 4. scrollHeight 与 offsetHeight offsetHeight是自身元素的高度,scrollHeight是 自身元素的高度+隐藏元素的高度。 <div id="container" style="background-color:silver; width:100px; height:100px; overflow:auto;"> <script> |
|
来自: 耍库 > 《吴延峰个人图书馆》