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:设置或获取位于对 ...
随机推荐
- 基于Xen实现一种domain0和domainU的应用层数据交互高效机制 - 3
继续 上一篇 的研究,结合 xen4.2.3 的代码分析,发现 xen4.2.3 的应用层工具库 tools 包含一个工具叫 libvchan ,其头文件描述如下: * This is a libra ...
- python搭建区块链
#!/usr/bin/env python # encoding: utf-8 ''' 我们要创建一个 Blockchain 类 ,他的构造函数创建了一个初始化的空列表(要存储我们的区块链),并且另一 ...
- SpringMvc+Spring+Mybatis+Maven整合
一.建立数据库表,使用generator自动生成相关代码: /* SQLyog Ultimate v11.24 (32 bit) MySQL - 5.1.62-community : Database ...
- HDU 5900 QSC and Master (区间DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意:给出序列$A_{i}.key$和$A_{i}.value$,若当前相邻的两个数$A_{ ...
- oracle free space
--Size of All Table Space --1. Used Space SELECT TABLESPACE_NAME,TO_CHAR(SUM(NVL(BYTES,0))/1024/1024 ...
- 有关奇葩的mex编程时的matlab出现栈内存错误的问题
错误提示信息 (ntdll.dll) (MATLAB.exe中)处有未经处理的异常:0xC0000374:堆已损坏 该错误的表现是,matlab调用.mexw64函数时,第一次调用正常,第二次调用出现 ...
- 关联模型中如果condition条件
在练习中,有一个user表和地址表,一对多的关系. 我的想法是,通过这个关联模型找出这个用户下面默认值字段为1的地址 控制器中 public function index(){ $User = D(' ...
- python里的“__all__ ”作用
转载:http://python-china.org/t/725 参考:http://www.cnblogs.com/alamZ/p/6943869.html 用 __all__ 暴露接口,这是一种约 ...
- eclipse进行Debug的时候,发出“java breakpoint unable to install breakpoint”错误
错误情况图: 问题的解决方法: 直接点击忽略掉:Don't tell me again 来自网上的答案~~ I had the same error message in Eclipse 3.4.1, ...
- django 分页django-pure-pagination
虽然django自带了一个paginator,但不是很方便,我们使用django-pure-pagination github地址https://github.com/jamespacileo/dja ...