offsetTop/offsetHeight scrollTop/scrollHeight 的区别
offsetTop/offsetHeight scrollTop/scrollHeight 这几个属性困扰了我N久,这次一定要搞定。
假设 obj 为某个 HTML 控件。
obj.offsetTop obj距离上方或上层控件的位置,整型,单位像素。
obj.offsetHeight obj自身的高度,整型,单位像素。
offsetTop 可以获得 HTML 元素距离上方或外层元素的位置,style.top 也是可以的,二者的区别是:
一、offsetTop 返回的是数字,而 style.top 返回的是字符串,除了数字外还带有单位:px。
二、offsetTop 只读,而 style.top 可读写。
三、如果没有给 HTML 元素指定过 top 样式,则 style.top 返回的是空字符串。
scrollTop 是“卷”起来的高度值,示例:
<div style="width:100px;height:100px;overflow:hidden;" id="p">
<div style="width:50px;height:300px;" id="t">如果为 p 设置了 scrollTop,这些内容可能不会完全显示。</div>
</div>
<script type="text/javascript">
var p = document.getElementById("p");
p.scrollTop = 10;
</script>
由于为外层元素 p 设置了 scrollTop,所以内层元素会向上卷。
scrollHeight 与 offsetHeight
offsetHeight是自身元素的高度,scrollHeight是 自身元素的高度+隐藏元素的高度(是不是可以理解成内层元素的offsetHeight值???)。
<div id="container" style=" width:100px; height:100px; overflow:auto;">
<p style=" height:250px; ">
别再做情人 做只猫 做只狗 不做情人 做只宠物至少可爱迷人 和你相交不浅无谓明日会被你憎
</p>
</div>
<script>
alert(document.getElementById("container").offsetHeight);
alert(document.getElementById("container").scrollHeight);
</script>
将依次输出100,250。因为已经指定了元素的height为100px,所以offsetHeight始终为100px;内部元素为 250px,而容器元素只有100px,那么还有150px的内容它无法显示出来,但它却是实际存在的,所以scrollHeight值为 100+150=250。
转:http://hi.baidu.com/lampers/item/1605adc13daa00350931c6fc
http://www.cnblogs.com/fullhouse/archive/2012/01/16/2324131.html
alert("网页可见区域宽:" + 'document.body.clientWidth.'+document.body.clientWidth);
alert("网页可见区域高:" + 'document.body.clientHeight.'+document.body.clientHeight);
alert("网页可见区域高 (包括边线的宽):" + 'document.body.offsetWidth.'+document.body.offsetWidth);
alert("网页可见区域高 (包括边线的高):" + 'document.body.offsetHeight.'+document.body.offsetHeight);
alert("网页正文全文宽:" + 'document.body.scrollWidth.'+document.body.scrollWidth);
alert("网页正文全文高:" + 'document.body.scrollHeight.'+document.body.scrollHeight);
alert("网页被卷去的高:" + 'document.body.scrollTop.'+document.body.scrollTop);
alert("网页被卷去的左:" + 'document.body.scrollLeft.'+document.body.scrollLeft);
alert("网页正文部分上:" + "window.screenTop."+window.screenTop);
alert("网页正文部分左:" + "window.screenLeft."+window.screenLeft);
alert("屏幕分辨率的高:" + "window.screen.height."+window.screen.height);
alert("屏幕分辨率的宽:" + "window.screen.width."+window.screen.width);
alert("屏幕可用工作区高度:" + "window.screen.availHeight."+window.screen.availHeight);
alert("屏幕可用工作区宽度:" + "window.screen.availWidth." +window.screen.availWidth);
offsetTop/offsetHeight scrollTop/scrollHeight 的区别的更多相关文章
- 关于offsetTop offsetHeight clientHeight scrollHeight scrollTop的区别研究
我是以chrome浏览器做的研究. 先看一段代码: <script> window.addEventListener('DOMContentLoaded',function(){ var ...
- offsetTop,offsetHeight,clientHeight,scrollHeight,scrollTop区别
这些高度相信很多同学都搞不清楚吧.这里我通过本地测试,发现了区别. 以聊天窗口为例. 元素(class='content')高度444px,其中上下padding分别是10px,margin为0.距离 ...
- jquery 对象的 height、innerHeight、outerHeight 的区别以及DOM 元素的 clientHeight、offsetHeight、scrollHeight、offsetTop、scrollTop
前言:jquery 对象的 height.innerHeight.outerHeight,还有 DOM 元素的 clientHeight.offsetHeight.scrollHeight.offse ...
- JS clientHeight,scrollHeight,offsetHeight,scrollTop,offsetTop概念
JS滚动页面到某一位置时触发指定事件能够增强用户体验或是提高性能,其中使用最多的场景是加载更多,当鼠标滚动至页面下方时,自动加载下一页的内容.另一个常用的场景是当用户滚动至页面某一地方时,页面会给出提 ...
- offsetHeight, clientHeight与scrollHeight的区别
在网上搜了一下,结论非常笼统,讲IE从不讲版本,因此自己做了测试并上传结论.以下结论皆是在标准模式下测试通过的,没有测试quirk模式. clientHeight 大部分浏览器对 clientHe ...
- clientHeight ,offsetHeight,style.height,scrollHeight有区别与联系
style.height 包括 元素的滚动条,不包括边框clientHeight 不包括元素的滚动条和边框,只有在没有元素的滚动条的情况下,style.height相等于clientHeightoff ...
- clientHeight / scrollHeight / offsetHeight 等属性的区别图
网页(内容)可见区域宽:document.body.clientWidth 网页(内容)可见区域高:document.body.clientHeight 即页面浏览器中可以看到内容的这个区域的高度,一 ...
- height、clientHeight、offsetHeight、scrollHeight、height()、 innerHeight()、outerHeight()等的区别
1.height height是css属性,这个属性定义元素内容区的高度,在内容区外面可以增加内边距.边框和外边距. 当 box-sizing: content-box 时,高度应用到元素的内容框. ...
- clientTop、offsetTop和scrollTop的区分
页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offse ...
随机推荐
- 十四、dbms_obfuscation_toolkit(用于加密和解密应用数据)
1.概述 作用:用于加密和解密应用数据,另外还可以生成密码检验和.通过加密输入数据,可以防止黑客或其他用户窃取私有数据;而通过结合使用加密和密码检验和,可以防止黑客破坏初加密的数据.当使用该包加密数据 ...
- ParentNodes、childNodes、children之间的区别
"parentNode" 常用来获取某个元素的父节点. 把 parentNodes 理解为容器, 在容器中有个子节点 例: <div id="parent" ...
- eclipse集群tomcat
eclipse集群tomcat 1. File -> new -> other 选择server. 2. 选择Apache下边对应的tomcat版本,配置tomcat名称即可.由于我本 ...
- 20165210 《网络对抗技术》week1 exp0 kali安装与配置
20165210 <网络对抗技术>week1 exp0 kali安装与配置 1. 安装过程: 从kali官网上下载如下图所示: 下载完成后打开VMware 点击创建新的虚拟机 弹出新虚拟机 ...
- C#设置System.Net.ServicePointManager.DefaultConnectionLimit,突破Http协议的并发连接数限制
在Http协议中,规定了同个Http请求的并发连接数最大为2. 这个数值,可谓是太小了. 而目前的浏览器,已基本不再遵循这个限制,但是Dot Net平台上的 System.Net 还是默认遵循了这个标 ...
- 實現多个checkbox單選功能(转)
<script type="text/javascript"> function check(obj) { var checkbox ...
- Linux下打开windows中文文本乱码问题
1. 查看文件的编码方式:file命令 $ file test_file.txt test_file.txt: ISO- text, with very long lines $ file train ...
- caffe官网的部分翻译及NG的教程
Caffe原来叫:Convolutional Architecture for Fast Feature Embedding 官网的个人翻译:http://blog.csdn.net/fengbing ...
- OK335xS knob driver hacking
/************************************************************************* * OK335xS knob driver hac ...
- PHP 关于empty和isset对于参数的判断结果
<?php class test{} $a1 = null; $a2 = ""; //$a3 = $a4 = 0; $a5 = '0'; $a6 = false; $a7 = ...