我是以chrome浏览器做的研究。

先看一段代码:

<script>
window.addEventListener('DOMContentLoaded',function(){
var node1 = document.querySelector('#father');
var node2 = document.querySelector('#child');
console.log('offsetTop==offsetHeight==scrollTop==scrollHeight==clientHeight');
console.log('father: '+node1.offsetTop+'=='+node1.offsetHeight+'=='+node1.scrollTop+'=='+node1.scrollHeight+'=='+node1.clientHeight);
console.log('child: '+node2.offsetTop+'=='+node2.offsetHeight+'=='+node2.scrollTop+'=='+node2.scrollHeight+'=='+node2.clientHeight);
/**
offsetTop:是本元素距上层元素且元素设置了postion=relative的距离,如果所有父级元素都没有设置postion。就是距body的距离。
计算:offsetTop = margin+top offsetHeight:是本元素底部到本元素顶部的距离。
计算:offsetHeight = content+padding+border scrollTop:是浏览器可视窗口顶端距页面顶部的距离。
计算: 无 scrollHeight: 是容器内所有元素及子元素的高度之和,如果没有子元素,即为自身高度+padding。
计算: 有子元素:包括所有子元素的(content+padding+border+margin)之和
无子元素:content+padding clientHeight: 是自身容器的高度。除去滚动条的宽度。
计算: content.height+padding-滚动条的宽度
*/
});
</script>
</head>
<body>
<div style="position:relative;" id="superFather">
<div style="width:500px;height:1000px;background:red;overflow:auto;" id="father">
<!-- <div style="width:500px;height:500px;background:green;padding:10px;margin:60px;border:5px solid #2EE008; opacity:0.5;position:absolute;top:50px;" id="child"> </div> -->
<div style="width:800px;height:500px;background:green;padding:10px;margin:60px;border:5px solid #2EE008; opacity:0.5;" id="child"> </div>
<div style="width:800px;height:500px;background:green;padding:10px;margin:60px;border:5px solid #2EE008; opacity:0.5;" id="child"> </div>
<div style="width:800px;height:500px;background:green;padding:10px;margin:60px;border:5px solid #2EE008; opacity:0.5;" id="child"> </div>
</div>
</div> </body>

所以,根据上面的解释输出结果为:

offsetTop:  farther=0,child = margin(60)

offsetHeight: farther = content(1000),child = content(500)+padding(20)+border(10)

scrollTop: 0,0

scrollHeight: farther=500*3+60*4+20*3+10*3,500+10*2

clientHeight: 100-17,500+10*2

所以整体输出:

总结一下算法:

offsetHeight:content+padding+border

offsetTop: margin+top

scrollHeight/clientHeight: 子元素没有超出情况下:content+padding 只是clientHeight在有滚动条的情况下,是要去除滚动条宽度。子元素超出之后:所有子元素的(content+padding+border+margin)之和

scrollTop:浏览器可视区域顶部到页面顶部的距离

参考链接:http://www.softwhy.com/forum.php?mod=viewthread&tid=8298

关于offsetTop offsetHeight clientHeight scrollHeight scrollTop的区别研究的更多相关文章

  1. offsetTop,offsetHeight,clientHeight,scrollHeight,scrollTop区别

    这些高度相信很多同学都搞不清楚吧.这里我通过本地测试,发现了区别. 以聊天窗口为例. 元素(class='content')高度444px,其中上下padding分别是10px,margin为0.距离 ...

  2. 彻底搞清楚DOM元素的height,offsetHeight,clientHeight,scrollHeight

    测试用例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  3. [DOM基础]offsetHeight,clientHeight,scrollHeight,innerHeight,outerHeight等属性的解释

    由于经常搞混这几个属性,所以查找资料总结一下,方便以后翻出来温习. 一.偏移量-以offset开头的 1.offsetHeight:元素在垂直方向上占用的空间大小,像素.包括元素的高度.可见的水平滚动 ...

  4. offsetHeight,clientHeight,scrollHeight,offsetY等属性的理解

    el.offsetHeight = height + padding + border(滚动条是在边框内的,自然也包括在内) el.clientHeight = 可视化看到的高度 (就是content ...

  5. jquery 对象的 height、innerHeight、outerHeight 的区别以及DOM 元素的 clientHeight、offsetHeight、scrollHeight、offsetTop、scrollTop

    前言:jquery 对象的 height.innerHeight.outerHeight,还有 DOM 元素的 clientHeight.offsetHeight.scrollHeight.offse ...

  6. offsetTop/offsetHeight scrollTop/scrollHeight 的区别

    offsetTop/offsetHeight   scrollTop/scrollHeight  这几个属性困扰了我N久,这次一定要搞定. 假设 obj 为某个 HTML 控件. obj.offset ...

  7. 理解clientX、clientY、offsetLeft、event.offsetTop、offsetWidth、offsetHeight、clientWidth、clientHeight、scrollTop、scrollHeight

    一.clientX和clientY 事件发生时,鼠标距离浏览器的可视区域的X.Y轴的位置,不包含滚动条的区域的部分.就算是页面进行了滚动,鼠标的坐标值还是参考可视区域的. 二.offsetLeft和o ...

  8. 搞懂offsetY、offsetTop、scrollTop、offsetHeight、scrollHeight

    先搞offsetTop,最难懂的就是它了 官方解释:返回当前元素的上边界到它的包含元素的上边界的偏移量,以像素为单位.这真TM坑爹啊!有木有!经过仔细研究查找得出结论:offsetTop是相对于离它最 ...

  9. offsetHeight, clientHeight与scrollHeight的区别

      在网上搜了一下,结论非常笼统,讲IE从不讲版本,因此自己做了测试并上传结论.以下结论皆是在标准模式下测试通过的,没有测试quirk模式. clientHeight 大部分浏览器对 clientHe ...

随机推荐

  1. ComponentName的意思

    ComponentName是用来打开其它应用程序中的Activity或服务的. 使用方法: Intent i=new Intent(); i.setComponent(new ComponentNam ...

  2. RMAN数据库恢复 之归档模式有(无)备份-丢失数据文件的恢复

    1.归档模式有备份,丢失数据文件的恢复归档模式有备份,不管丢失什么数据文件,直接在RMAN下RESTOER--->RECOVER--->OPEN即可. RMAN> STARUP MO ...

  3. VLC播放器架构剖析

    VLC采用多线程并行解码架构,线程之间通过单独的一个线程控制所有线程的状态,解码器采用filter模式.组织方式为模块架构 模块简述:libvlc                  是VLC的核心部分 ...

  4. 如何测试sql语句性能,提高执行效率

    有时候我们经常为我们的sql语句执行效率低下发愁,反复优化后,可还是得不到提高 那么你就用这条语句找出你sql到底是在哪里慢了 示例: SET STATISTICS io ON        SET ...

  5. Android中Menu的基本用法

    一. 使用xml定义Menu 菜单资源文件必须放在res/menu目录中.菜单资源文件必须使用<menu>标签作为根节点.除了<menu>标签外,还有另外两个标签用于设置菜单项 ...

  6. JAVA多线程下,获取递增的序列号

    场景描述: 1,目前我们的系统可以简单归纳成MVC的架构模式 2,每个前端的请求过来,都会在C层开启事务,最后处理结束后,也在在C层关闭事务(实际是在C层的底层统一做了事务的开启和提交):      ...

  7. Router和History (路由控制)-backbone

    Router和History (路由控制) Backbone.Router担任了一部分Controller(控制器)的工作,它一般运行在单页应用中,能将特定的URL或锚点规则绑定到一个指定的方法(后文 ...

  8. jQuery工具函数上

    1.字符串操作 <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...

  9. wampserver 2.2装好后80端口未被占用,却打不开localhost

    在windows server 2003中装好wampserver2.2后打不开localhost,点击服务全部启动(颜色是橙色)也是打不开,我解决的原因是:安装mysql中sevice中的安装测试服 ...

  10. JVM GC之一找出不可达对象并回收

    JAVA运行时数据区域 1.程序计数器:当前线程所执行的字节码的行号指示器.一个处理器只会执行一条线程中的指令,为了线程切换后能回复到正确的执行位置,所以每条线程都需要一个独立的计数器.各条线程之间互 ...