clientHeight / scrollHeight / offsetHeight 等属性的区别图
2011-10-29 15:34
429 查看
取document.body.scrollTop 值时 一直为0, 是因为:
页面具有 DTD(或者说指定了 DOCTYPE)时,使用 document.documentElement。
页面不具有 DTD(或者说没有指定了 DOCTYPE)时,使用 document.body。
在 IE 和 Firefox 中均是如此。
为了兼容(不管有没有 DTD),可以使用如下代码:
var scrollTop = window.pageYOffset
|| document.documentElement.scrollTop
|| document.body.scrollTop
|| 0;
下面是一个右侧动态悬浮的例子: 兼容firefox 和 IE 7 (IE 版本太多了 ,测试不过来)
<script type="text/javascript" language="javascript">
function scall() {
var scrollTop = window.pageYOffset
|| document.documentElement.scrollTop
|| document.body.scrollTop
|| 0;
$get("divChat").style.top = (scrollTop + 300)+"px";
}
window.onscroll = scall;
window.onresize = scall;
window.onload = scall;
</script>
<div id="divChat" style="position: absolute;right:0;top:300px;" title='Click here to chat with us.'>
Test chat div
</div>
网页(内容)可见区域宽:document.body.clientWidth
网页(内容)可见区域高:document.body.clientHeight 即页面浏览器中可以看到内容的这个区域的高度,一般是最后一个工具条以下到状态栏以上的这个区域,与页面内容无关。
网页可见区域宽:document.body.offsetWidth (包括边线的宽)
网页可见区域高:document.body.offsetHeight (包括边线的宽)
网页正文全文宽:document.body.scrollWidth
网页正文全文高:document.body.scrollHeight
IE、Opera 认为 scrollHeight 是网页内容实际高度,可以小于 clientHeight。
NS、 FF 认为 offsetHeight 和 scrollHeight 都是网页内容高度,只不过当网页内容高度小于等于 clientHeight 时,scrollHeight 的值是 clientHeight,而 offsetHeight 可以小于 clientHeight。
网页被卷去的高:document.body.scrollTop
网页被卷去的左:document.body.scrollLeft
网页正文部分上:window.screenTop
网页正文部分左:window.screenLeft
屏幕分辨率的高:window.screen.height
屏幕分辨率的宽:window.screen.width
屏幕可用工作区高度:window.screen.availHeight
屏幕可用工作区宽度:window.screen.availWidth
![](http://pic002.cnblogs.com/images/2011/85285/2011102915280329.jpg)
原文链接:http://blog.163.com/it_yinliqing/blog/static/784972712008111011843895/
页面具有 DTD(或者说指定了 DOCTYPE)时,使用 document.documentElement。
页面不具有 DTD(或者说没有指定了 DOCTYPE)时,使用 document.body。
在 IE 和 Firefox 中均是如此。
为了兼容(不管有没有 DTD),可以使用如下代码:
var scrollTop = window.pageYOffset
|| document.documentElement.scrollTop
|| document.body.scrollTop
|| 0;
下面是一个右侧动态悬浮的例子: 兼容firefox 和 IE 7 (IE 版本太多了 ,测试不过来)
<script type="text/javascript" language="javascript">
function scall() {
var scrollTop = window.pageYOffset
|| document.documentElement.scrollTop
|| document.body.scrollTop
|| 0;
$get("divChat").style.top = (scrollTop + 300)+"px";
}
window.onscroll = scall;
window.onresize = scall;
window.onload = scall;
</script>
<div id="divChat" style="position: absolute;right:0;top:300px;" title='Click here to chat with us.'>
Test chat div
</div>
网页(内容)可见区域宽:document.body.clientWidth
网页(内容)可见区域高:document.body.clientHeight 即页面浏览器中可以看到内容的这个区域的高度,一般是最后一个工具条以下到状态栏以上的这个区域,与页面内容无关。
网页可见区域宽:document.body.offsetWidth (包括边线的宽)
网页可见区域高:document.body.offsetHeight (包括边线的宽)
网页正文全文宽:document.body.scrollWidth
网页正文全文高:document.body.scrollHeight
IE、Opera 认为 scrollHeight 是网页内容实际高度,可以小于 clientHeight。
NS、 FF 认为 offsetHeight 和 scrollHeight 都是网页内容高度,只不过当网页内容高度小于等于 clientHeight 时,scrollHeight 的值是 clientHeight,而 offsetHeight 可以小于 clientHeight。
网页被卷去的高:document.body.scrollTop
网页被卷去的左:document.body.scrollLeft
网页正文部分上:window.screenTop
网页正文部分左:window.screenLeft
屏幕分辨率的高:window.screen.height
屏幕分辨率的宽:window.screen.width
屏幕可用工作区高度:window.screen.availHeight
屏幕可用工作区宽度:window.screen.availWidth
![](http://pic002.cnblogs.com/images/2011/85285/2011102915280329.jpg)
原文链接:http://blog.163.com/it_yinliqing/blog/static/784972712008111011843895/
相关文章推荐
- clientHeight / scrollHeight / offsetHeight 等属性的区别图
- clientHeight / scrollHeight / offsetHeight 等属性的区别图
- clientHeight / scrollHeight / offsetHeight 等属性的区别图
- clientHeight / scrollHeight / offsetHeight 等属性的区别图
- clientHeight / scrollHeight / offsetHeight 等属性的区别图
- clientHeight / scrollHeight / offsetHeight 等属性的区别图示( 作者:Choy Lv)
- 关于offsetTop offsetHeight clientHeight scrollHeight scrollTop的区别研究
- offsetWidth clientWidth scrollWidth scrollTop scrollLeft等属性的细微区别
- 区分总结innerHeight与clientHeight、innerWidth与clientWidth、scrollLeft与pageXOffset等属性
- 区分innerHeight与clientHeight、innerWidth与clientWidth、scrollLeft与pageXOffset等属性
- 区别client、offset、scroll系列以及event的几个距离属性
- Javascritp client,offset,scroll的区别,这里只说width和height
- 图示offsetWidth clientWidth scrollWidth scrollTop scrollLeft等属性的细微区别
- 区分innerHeight与clientHeight、innerWidth与clientWidth、scrollLeft与pageXOffset等属性
- clientWidth clientHeight scrollWidth scrollHeight offsetWidth offsetHieght的区别
- height clientHeight scrollHeight offsetHeight的大致区别
- js的element中,属性以scroll、client和offset为前缀的属性的区别
- offsetWidth/offsetHeight,clientWidth/clientHeight与scrollWidth/scrollHeight的区别
- JS属性 ClientTop,scrollTop,offsetWidth,ClientWidth,clientLeft,padding,border,top,margin区别
- scrollTop clientTop offsetTop scrollHeight clientHeight clientWidth的区别及用法