这几个属性做滚动时会经经常使用到。现总例如以下:

首先定义一个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的差别及使用方法的更多相关文章

  1. 花点时间搞清top、clientTop、scrollTop、offsetTop

    [转贴]花点时间搞清top.clientTop.scrollTop.offsetTop   scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对象左边界和窗口中目前 ...

  2. JavaScript--clientX,clientY、pageX,pageY、offsetLeft,offsetTop/offsetWidth,offsetHeight、scrollLeft,scrollTop/scrollWidth,scrollHeight、clientHeight,clientWidth区别

    /*在事件的内部console.dir(event)*/ /** * 事件对象event * clientX/clientY 获取鼠标基于浏览器窗口(可视区域的坐标位置)全兼容 * * pageX/p ...

  3. Height、clientHeight、scrollHeight、offsetHeight 、scrollTop、offsetTop

    Height 返回当前文档中的<body>元素的高度 clientHeight 对于没有定义CSS或者内联布局盒子的元素为0,否则,它是元素内部的高度(单位像素),包含内边距,但不包括水平 ...

  4. 关于offsetTop offsetHeight clientHeight scrollHeight scrollTop的区别研究

    我是以chrome浏览器做的研究. 先看一段代码: <script> window.addEventListener('DOMContentLoaded',function(){ var ...

  5. javascript中top、clientTop、scrollTop、offsetTop的讲解(转载加总结)

    scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离 scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最 ...

  6. html中offsetTop、clientTop、scrollTop、offsetTop

    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...

  7. html中offsetTop、clientTop、scrollTop、offsetTop各属性

    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...

  8. offsetTop、clientTop、scrollTop、offsetTop各属性介绍

    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...

  9. [转]html中offsetTop、clientTop、scrollTop、offsetTop各属性介绍

    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...

随机推荐

  1. Distributed Cache Coherence at Scalable Requestor Filter Pipes that Accumulate Invalidation Acknowledgements from other Requestor Filter Pipes Using Ordering Messages from Central Snoop Tag

    A multi-processor, multi-cache system has filter pipes that store entries for request messages sent ...

  2. 将本地访问ip映射成域名

    通过修改以下地址 C:\WINDOWS\system32\drivers\etc\hosts 加进你自己的如: 192.168.1.101  www.helloworld.com 配置nginx代理u ...

  3. VS2015开发的C++应用如何不依赖Visual C++ 2015 redistributable?

    1,“项目属性,C/C++,代码生成”,“运行库改”为“多线程(/MT)”.* MT开头的是静态引用,MD开头的是动态引用,d结尾的是Debug调试版本,没有d的是Release发布版本,所以就一 共 ...

  4. 牛客小白月赛3 F 异或【区间交集】

    链接:https://www.nowcoder.com/acm/contest/87/F 来源:牛客网 题目描述 Cwbc想测试一下他的加密协议,以便防止其他人偷看他给XHRlyb的信. Cwbc提出 ...

  5. Codeforces Round #446 (Div. 2) B. Wrath【模拟/贪心】

    B. Wrath time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  6. (转)NSArray类的使用

    NSArray的基本应用总结 NSString *string1 = @"two"; NSString *string2 = @"one"; //创建数组 NS ...

  7. 每天一个linux命令8之grep高级篇

    1语法       grep -[acinv] '搜索内容串' filename -a 以文本文件方式搜索-c 计算找到的符合行的次数-i 忽略大小写-n 顺便输出行号-v 反向选择,即找 没有搜索字 ...

  8. zookeeper 学习笔记2

    ephemeral 英[ɪˈfemərəl]美[ɪˈfɛmərəl]adj. 朝生暮死; 短暂的,瞬息的; 朝露; 一年生; ZooKeeper Watcher 机制 集群状态监控示例 为了确保集群能 ...

  9. 常用VBA小技巧

    用对话框选取文件路径(单个文件) 删除导入csv等文本文件后留下的 Data connections 增加新的工作表并并命名 Worksheets.Add(After:=Worksheets(Work ...

  10. 为什么代理属性设置成assign为了防止生成保留环来

    循环引用 全部的引用计数系统, 都存在循环应用的问题, 比如以下的引用关系: 1. 对象a创建并引用到了对象b 2. 对象b创建并引用到了对象c 3. 对象c创建并引用到了对象b 这时候b和c的引用计 ...