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 ...
随机推荐
- golang简易版聊天室
功能需求: 创建一个聊天室,实现群聊和单聊的功能,直接输入为群聊,@某人后输入为单聊 效果图: 群聊: 单聊: 服务端: package main import ( "fmt" ...
- I/O流+统计文件词频
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 2018.11.12 RF debug
1 SG setting 2 Date 3 0-1 4 ASK 5 Power supply 6 SG - filter 7 NRF905- demodulation 8 RX test 9 ...
- java android 读写西门子PLC数据,包含S7协议和Fetch/Write协议,s7支持200smart,300PLC,1200PLC,1500PLC
本文将使用一个gitHub开源的组件技术来读写西门子plc数据,使用的是基于以太网的TCP/IP实现,不需要额外的组件,读取操作只要放到后台线程就不会卡死线程,本组件支持超级方便的高性能读写操作 gi ...
- BZOJ4976:宝石镶嵌(DP&思维)
Description 魔法师小Q拥有n个宝石,每个宝石的魔力依次为w_1,w_2,...,w_n.他想把这些宝石镶嵌到自己的法杖上,来提升 法杖的威力.不幸的是,小Q的法杖上宝石镶嵌栏太少了,他必须 ...
- HDU1556 线扫
昨天睡得太晚,今天又在看新算法,明天事情也多,烦,所以今天刷刷水题就过去了. 叫我用线段树,我反而搞不来 #include<cstdio> #include<cstdlib> ...
- ubuntu查看进程端口号及运行的程序
查看进程端口号及运行的程序:netstat -atunp 根据端口号来查看进程号:lsof -i:16031
- 向requestAnimationFrame的回调函数中传递参数
其实跟setTimeout类似,我们知道传参传的是一个函数,那么我们是不是可以用一个匿名函数来包裹这个函数的执行呢function fn(fc){ console.log('fc:',fc) fc++ ...
- Luogu 1452 Beauty Contest
Luogu 1452 Beauty Contest 求平面最远点对,先求出凸包,再找凸包的直径. 使用旋转卡壳,直径一定出现在对踵点对间.比较不同点到同一直线距离可以用叉积算三角形面积来比较. 实现时 ...
- HDU3047 Zjnu Stadium 【带权并查集】
HDU3047 Zjnu Stadium Problem Description In 12th Zhejiang College Students Games 2007, there was a n ...