scrollTop clientTop offsetTop scrollHeight clientHeight clientWidth的差别及使用方法
这几个属性做滚动时会经经常使用到。现总例如以下:
首先定义一个div。样式例如以下:
<style>
*{ margin:0px; padding:0px;}
body{ margin:0px; padding:10px; border:solid 10px #69F;}
.aa{ margin:20px auto 0px; width:100px; height:50px; overflow:auto; border:solid 1px #cccccc; padding:0px 10px;}
</style>
<script type="text/javascript">
window.onload = test;
function test(){
var aa = document.getElementById("aa");
//aa.scrollTop = 60;
//alert(aa.scrollTop); //假设滚动栏不滚动时为0。比方滚动20,则弹出20
//alert(document.body.scrollHeight);//整个屏幕的可显示网页的高度。即一屏的高度 666
//alert(document.body.clientTop); //10 即body的上边框宽度
//alert(aa.style.top); //什么也不弹出 ???
//alert(aa.scrollTop); //60 假设上面aa.scrollTop不设置为60。即滚动栏不滚动时,弹出0
//alert(aa.offsetTop); //40 div偏移顶部窗体的距离
//alert(aa.clientTop); //1 即div的上边框
//alert(aa.scrollHeight); //380 div内容的总高度。即不设高度,让内容自适应得到的高度 + padding
//alert(aa.offsetHeight); //72 div的高度加上上下padding再加上 border 即height + paading + border
//alert(aa.clientHeight); //70 div的高度加上上下padding 即height + padding
//alert(document.body.clientHeight) //112 屏幕中显示内容的高度
//alert(aa.clientWidth); //103 div的宽度减去滚动栏的宽度再加上左右padding
//alert(aa.scrollWidth); //103 div的宽度减去滚动栏的宽度再加上左右padding ,和 aa.clientWidth一样
//alert(aa.offsetWidth); //122 div的宽度加上padding + border 即width + padding + border
//alert(document.body.clientWidth); //1346 整个屏幕的宽度减去body的左右border 即1366 - 20
//alert(document.body.offsetWidth); //1366 整个屏幕的宽度
alert(aa.clientLeft); //1 即div的左边框
alert(aa.scrollLeft); //0 滚动栏没有向右滚动。所以弹出0
alert(aa.offsetLeft); //ie和谷歌为622。火狐为612 即ie和谷歌包括body的左边框,而火狐不包括body的左边框。总之
都是div距窗体左边的距离。
alert(document.body.clientLeft); //10 即body的左边框
}
</script>
div.scrollTop:假设上面aa.scrollTop不设置为60,即滚动栏不滚动时,弹出0
div.offsetTop:div偏移顶部窗体的距离
div.clientTop:div的上边框
div.scrollHeight:div内容的总高度,即不设高度。让内容自适应得到的高度 + padding
div.offsetHeight:整个div加上上下边框的高度再加上border。
即整个div的高度。 即height + padding + border
div.clientHeight:css样式文件中规定的div的高度再加上上下padding。不加上下边框的高度。即height + padding
document.body.clientHeight:屏幕中显示内容的高度 不加上body的上下边框
div.clientWidth:div的宽度减去滚动栏的宽度再加上左右padding
div.scrollWidth:和div.clientWidth一样。
div.offsetWidth:div的宽度加上padding + border 即width + padding + border
document.body.clientWidth:整个屏幕的宽度。即屏幕的宽度的显示分辨率的宽度减去body的左右边框
document.body.offsetWidth:整个屏幕的宽度。即屏幕的宽度的显示分辨率的宽度。1366
div.clientLeft:即div的左边框
div.scrollLeft:滚动栏向右滚动的距离
div.offsetLeft:div距窗体的左边框,ie和谷歌包括body的左边框。而火狐不包括body的左边框。
即在火狐中比在ie和谷歌降低body的左边框
document.body.clientLeft:body的左边框
scrollTop clientTop offsetTop scrollHeight clientHeight clientWidth的差别及使用方法的更多相关文章
- 花点时间搞清top、clientTop、scrollTop、offsetTop
[转贴]花点时间搞清top.clientTop.scrollTop.offsetTop scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对象左边界和窗口中目前 ...
- JavaScript--clientX,clientY、pageX,pageY、offsetLeft,offsetTop/offsetWidth,offsetHeight、scrollLeft,scrollTop/scrollWidth,scrollHeight、clientHeight,clientWidth区别
/*在事件的内部console.dir(event)*/ /** * 事件对象event * clientX/clientY 获取鼠标基于浏览器窗口(可视区域的坐标位置)全兼容 * * pageX/p ...
- Height、clientHeight、scrollHeight、offsetHeight 、scrollTop、offsetTop
Height 返回当前文档中的<body>元素的高度 clientHeight 对于没有定义CSS或者内联布局盒子的元素为0,否则,它是元素内部的高度(单位像素),包含内边距,但不包括水平 ...
- 关于offsetTop offsetHeight clientHeight scrollHeight scrollTop的区别研究
我是以chrome浏览器做的研究. 先看一段代码: <script> window.addEventListener('DOMContentLoaded',function(){ var ...
- javascript中top、clientTop、scrollTop、offsetTop的讲解(转载加总结)
scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离 scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最 ...
- html中offsetTop、clientTop、scrollTop、offsetTop
HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...
- html中offsetTop、clientTop、scrollTop、offsetTop各属性
HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...
- offsetTop、clientTop、scrollTop、offsetTop各属性介绍
HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...
- [转]html中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...
随机推荐
- strtol函數的用法 atof, atoi, atol, strtod, strtoul
相关函数: atof, atoi, atol, strtod, strtoul表头文件: #include <stdlib.h>定义函数: long int strtol(const ch ...
- 谈谈dpdk应用层包处理程序的多进程和多线程模型选择时的若干考虑
看到知乎上有个关于linux多进程.多线程的讨论:http://www.zhihu.com/question/19903801/answer/14842584 自己项目里也对这个问题有过很多探讨和测试 ...
- 戴文的Linux内核专题:03 驱动程序【转】
转自:http://www.lai18.com/content/432194.html 驱动程序是使内核能够沟通和操作硬件或协议(规则和标准)的小程序.没有驱动程序,内核不知道如何与硬件沟通或者处理协 ...
- PL/SQL&存储过程||存储函数&触发器
plsql 有点:交互式 非过程化 数据操纵能力强 自动导航语句简单 调试简单 想率高 声明类型的方式 1.基本类型 2.引用变量 3.记录型变量 基本格式 declare 声明 b ...
- Python的程序结构[8] -> 装饰器/Decorator -> 装饰器浅析
装饰器 / Decorator 目录 关于闭包 装饰器的本质 语法糖 装饰器传入参数 1 关于闭包 / About Closure 装饰器其本质是一个闭包函数,为此首先理解闭包的含义. 闭包(Clos ...
- 【bzoj1562】【[NOI2009]变换序列】匈牙利算法的性质利用
(上不了p站我要死了,侵权度娘背锅) Description Input Output Sample Input 5 1 1 2 2 1 Sample Output 1 2 4 0 3 HINT 30 ...
- 更改vsftpd默认的21端口
vsftpd默认的端口是21 我想更改为别的端口 那么首先编辑 vsftpd的配置文件 /etc/vsftpd/vsftpd.conf 添加监听端口 listen_port **** 然后修改ftp的 ...
- 弱电系统标准CAD图例识图讲解
弱电系统标准CAD图例识图讲解 http://www.360doc.com/content/17/0317/16/33642774_637680009.shtml
- 转换vmware的vmdk格式到qcow2或者raw格式
qemu-img convert xxxx-disk1.vmdk -f vmdk -O qcow2 xxxx-disk1.qcow2 qemu-img convert xxxx-disk1.vm ...
- 【maven】maven的web项目打包报错:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK
打包过程中报错如下: No compiler is provided in this environment. Perhaps you are running on a JRE rather than ...