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 ...
随机推荐
- 添加git 忽略文件
在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交. Git 忽略文件提交的方法 有三种方法可以实现忽略Gi ...
- eureka -2 - 重要配置
Server 端配置 eureka.client.registerWithEureka :是否将自己注册到Eureka Server,默认是true,如果是单节点部署,切是server端,则设置成fa ...
- 2018.11.14 Chopin’s
The lineaments of Chopin’s肖邦 short, dramatic life are familiar to most classical-music enthusiasts. ...
- JUnit出错,却没有显示任何报错信息【待解答】
JUnit测试代码如下: 原因分析: JUnit测试单元里,测试函数好像不能带参数? 解决办法: 发现测试函数testBookShopDaoUpdateBookStock(int isbn)里的参数i ...
- Suffix(hash+lcp+二分)
题目链接: Suffix Consider n given non-empty strings denoted by s1 , s2 , · · · , sn . Now for each of th ...
- ScrollView中嵌套ExpandableListView
为了让SrollView中嵌套ExpandableListView,我们就得改ListView的高度,所以写一个类继承于ExpandableListview: import android.conte ...
- iOS runloop 自定义输入源
创建自定义输入源需要定义以下内容 1)输入源要处理的信息 2)使感兴趣的客户端知道如何和输入源交互的调度例程 3)处理其他任何客户发送请求的例程 4)使输入源失效的取消例程 上图的处理流程:主线程(M ...
- 创建第一个python程序:‘Hello World!’
安装好python解释器就可以创建第一个仪式程序Helloworld了 1.Python程序的3种运行方式 1.1.Python解释器直接运行 在Windows或者Linux命令行输入python,进 ...
- Postfix常用命令和邮件队列管理(queue)
本文主要介绍一下postfix的常用命令及邮件队列的管理: Postfix有以下四种邮件队列,均由管理队列的进程统一进行管理: maildrop:本地邮件放置在maildrop中,同时也被拷贝到inc ...
- xz文件的解压缩
前情 偶然发现了一个新的压缩文件类型 .xz 解决 其实很简单 xz -d ***.xz之后文件将会变为tar后缀 tar xvf ***.tar即可 另外 tar解压缩的时候,j代表bzip2,z代 ...