花点时间搞清top、postop、scrolltop、scrollHeight、offsetHeight
来源:互联网 发布:网络继续教育报名系统 编辑:程序博客网 时间:2024/05/01 23:09
1. top
此属性仅仅在对象的定位(position)属性被设置时可用。否则,此属性设置会被忽略。
<p style="background-color:silver; position:absolute; top:-5px;">测试top</p>
</div>
上面是一个段落P包含在一个DIV内,可以看到P的top设置为-5px后,它的上边距超过了容器DIV的上边距,超过的这段距离就是设置的5px。
需要注意的是,DIV和P这一对包含元素,都需要设置position为absolute才能得到想要的结果,假如父元素不设置,则子元素的参照将是更上层定义过position的元素,直到整个文档;
2. posTop
posTop的数值其实和top是一样的,但区别在于,top固定了元素单位为px,而posTop只是一个数值(这一点可以通过alert("top="+id.style.top)和alert("posTop="+id.style.posTop)来证明),因此一般使用posTop来进行运算。
<p id="test" style="background-color:silver; position:absolute;">测试posTop</p>
</div>
<script>
test.style.posTop = 15+8;
alert("top="+test.style.top);
alert("posTop="+test.style.posTop);
</script>
无论你使用top或posTop来赋值,最后的结果都是一致的
3. scrollTop
<p style="background-color:red;">
别再做情人 做只猫 做只狗 不做情人 做只宠物至少可爱迷人 和你相交不浅无谓明日会被你憎</p>
</div>
<script>
container.scrollTop = 12;
</script>
这一段文本在这个100*100的DIV内无法完全显示,所以设置了overflow为auto,它会出现一个上下方向的滑动框,假如没有设置id.scrollTop属性的话,默认情况下滑块位置在顶端。而设置了scrollTop值为12后,滑块的位置改变了,默认显示是卷过了12个象素的文本。如果设置overflow为hidden,则将会无法显示顶部12个象素的文本。
注意设置方式是id.scrollTop,而不是id.style.scrollTop。
4. scrollHeight 与 offsetHeight
offsetHeight是自身元素的高度,scrollHeight是 自身元素的高度+隐藏元素的高度。
<p style="background-color:red; height:250px; ">
别再做情人 做只猫 做只狗 不做情人 做只宠物至少可爱迷人 和你相交不浅无谓明日会被你憎</p>
</div>
<script>
alert(container.offsetHeight);
alert(container.scrollHeight);
</script>
将依次输出100,250。因为已经指定了元素的height为100px,所以offsetHeight始终为100px;内部元素为250px,而容器元素只有100px,那么还有150px的内容它无法显示出来,但它却是实际存在的,所以scrollHeight值为100+150=250。
- 花点时间搞清top、postop、scrolltop、scrollHeight、offsetHeight
- 花点时间搞清top、postop、scrolltop、scrollHeight、offsetHeight
- 花点时间搞清top、postop、scrolltop、scrollHeight、offsetHeight
- 花点时间搞清top、postop、scrolltop、scrollHeight、offsetHeigh
- 搞清top、postop、scrolltop、scrollHeight、offsetHeight
- 搞清top、postop、scrolltop、scrollHeight、offsetHeight
- 搞清top、postop、scrolltop、scrollHeight、offsetHeight
- top、postop、scrolltop、scrollHeight、offsetHeight
- top、postop、scrolltop、scrollHeight、offsetHeight
- top、postop、scrolltop、offsetTop、scrollHeight、offsetHeight、clientHeight
- top、postop、scrolltop、offsetTop、scrollHeight、offsetHeight、clientHeight
- top、postop、scrolltop、offsetTop、scrollHeight、offsetHeight、clientHeigh
- 搞清楚top、postop、scrolltop、scrollHeight、offsetHeight 的用法
- 页面位置 top、postop、scrolltop、offsetTop、scrollHeight、offsetHeight、clientHeight
- clientHeight/scrollHeight/offsetHeight/offsetParent/offsetTop/offsetHeight/scrollTop
- scrollWidth,offsetWidth,clientWidth,width;scrollHeight,offsetHeight,clientHeight,height;offsetTop,scrollTop,top;offsetLeft,scrol
- 搞清top、clientTop、scrollTop、offsetTop
- clientHeight、offsetHeight、scrollHeight、scrollTop的区别
- IE表单在DOM结构被修改后,checkbox和radio取值问题
- devfs,sysfs
- cobol学习3--语法与文法(1)
- cobol学习4--语法与文法(2)
- 大学生PK农民工
- 花点时间搞清top、postop、scrolltop、scrollHeight、offsetHeight
- EXT gridpanel自适应高度、宽度
- C语言声明学习之中级篇
- C++基本功之 对象序列化
- 使用SQL Server 数据库基础
- 【转】 C++中extern “C”含义深层探索
- WinCE5.0的一个EDB应用程序Debug版可以运行Release版就是老出错
- linux下的进程通信
- 制作随鼠标一起移动的层