在html里,width与height是最常用也是最基础的两个属性,因此,在js里,我们也经常需要操作这两个属性。js关于这两个属性提供了client*,offset*与scroll*,很多同学搞不清楚这三者之间的区别,经常望着这三个属性满脸问号,不知道该用哪个。所以今天就来看一下这三个属相的区别。

JUST SHUTUP AND SHOW ME YOUR CODE!!

ok,不多说话,看代码。

<style>
.out{
width:100px;
height:100px;
background:red;
}
</style>
<body>
<div class="out"></div>
</body>

我们这里创建了一个宽100,高100的红色方块,没有border,没有margin,也没有padding。我们先看这种情况下三个属性的值

var out = document.querySelector('.out');
console.log(out.clientHeight); //100
console.log(out.offsetHeight); //100
console.log(out.scrollHeight); //100

恩,值都一样,似乎看不出来什么,现在我们加一个padding

<style>
.out{
width:100px;
height:100px;
background:red;
padding:20px;

}
</style>
<body>
<div class="out"></div>
</body>

方块更大了,我们看一下这种情况下三个属性的值

var out = document.querySelector('.out');
console.log(out.clientHeight); //140
console.log(out.offsetHeight); //140
console.log(out.scrollHeight); //140

恩,值依然都相同,都是可视区高度+padding(不是content的高度+padding,原因一会说),现在我们再加一个border

<style>
.out{
width:100px;
height:100px;
background:red;
padding:20px;
border:10px solid black;

}
</style>
<body>
<div class="out"></div>
</body>

方块外面多了一个10像素的边框,我们再看一下三个属性的值

var out = document.querySelector('.out');
console.log(out.clientHeight); //140
console.log(out.offsetHeight); //160
console.log(out.scrollHeight); //140

注意,这时候发生变化了,clientHeight和scrollHeight没变化,offsetHeight变成了160!

观察到这里我们可以得到一个简易的结论:

clientHeight = 可视区高度 + padding

offsetHeight = content高度 + padding + border

scrollHeight = 可视区高度 + padding

仔细,仔细观察上面的结论(表述未必完全准确,待会会解释),scrollHeight与clientHeight一样(没有滚动的情况下),先不说,看offsetHeight与clientHeight。

offsetHeight相比clientHeight多了boder的宽度,然而可视区高度content高度又有什么不同呢?

看下面的图,我强制给方块添加了滚动条

<style>
.out{
width:100px;
height:100px;
background:red;
padding:20px;
border:10px solid black;
overflow:scroll;

}
</style>
<body>
<div class="out"></div>
</body>

此时的三个属性值为

var out = document.querySelector('.out');
console.log(out.clientHeight); //128
console.log(out.offsetHeight); //160
console.log(out.scrollHeight); //128

绿色的线表示可视区高度,蓝色的线表示content的高度

由于方块出现了滚动条,滚动条占用了一定的大小,可视区因此变小了,所以此时的方块,可视区高度 < content高度

了解了这一点,我们就知道,offsetHeight并不是仅仅比clientHeight多了border的宽度,即使没有border,但是有滚动条的情况下,clientHeight是要小于offsetHeight的

下面再来看scrollHeight

为外面的方块添加一个子方块

<style>
.out{
width:100px;
height:100px;
background:red;
padding:20px;
border:10px solid black;
}
.inner{
width:200px;
height:200px;
background:blue;
}

</style>
<body>
<div class="out">
<div class="inner"></div>
</div>
</body>

此时的scrollHeight的值为:

var out = document.querySelector('.out');
console.log(out.scrollHeight); //220

值为220,即scrollHeight = 内部元素的实际高度(content+padding+border+margin) + 父元素的padding-top值

注:当有滚动条时,padding-bottom和padding-right是无效的,不会进行计算,也不会进行渲染(chrome除外)

再次注意,这里计算时,对父元素的padding值应该只计算一个(即使父元素上下padding都设置了值),可以认为只计算padding-top值(实验证明确实计算的是padding-top值,如果不设置padding-top,只设置padding-bottom,那该值是无效的)

截止到目前为止,mac上的chrome(版本:51.0.2704.106 (64-bit))对scrollHeight的计算和渲染应该是错误的(不确定是不是错误,欢迎指正),因为chrome计算scrollHeight时,计算了padding-bottom,公式如下:

scrollHeight = 内部元素的实际高度(content+padding+border+margin) + 父元素的padding-top值 + 父元素的padding-bottom值

chrome不仅是这样计算的,也是这样渲染的!!

  

如上图,padding-bottom在mac上的chrome上起了作用,而在其他浏览器上没有作用(padding-right也是如此)


以上是关于height的三个属性的一些说明,width的三个属性同理,就不再多说,这里可以总结一下:

clientHeight = 可视区高度 + padding

offsetHeight = content高度 + padding + border

当内部元素高度超出了外部元素高度时

  scrollHeight = 内部元素的实际高度(content+padding+border+margin) + 父元素的padding-top值

当内部元素高度小于外部元素高度是

  scrollHeight = 可视区高度 + padding


既然说完了这三个属性,就顺便说说相关的clientTop,offsetTop,和scrollTop吧

clientTop是指当前可视对象距离上一级元素的距离,因为当前可视对象不包含border,所以通常来说,clientTop就等于border-top值

offsetTop是指当前元素到body元素的距离,因为当前元素包含了border,所以计算时要从margin值开始,依次向外加

scrollTop是指可滚动元素超出当前窗口显示范围的高度,超出多少与当前窗口的border的宽度无关

clientHeight,offsetHeight与scrollHeight的相关知识的更多相关文章

  1. jquery 对象的 height、innerHeight、outerHeight 的区别以及DOM 元素的 clientHeight、offsetHeight、scrollHeight、offsetTop、scrollTop

    前言:jquery 对象的 height.innerHeight.outerHeight,还有 DOM 元素的 clientHeight.offsetHeight.scrollHeight.offse ...

  2. 搞清clientHeight、offsetHeight、scrollHeight、offsetTop、scrollTop

    每个HTML元素都具有clientHeight offsetHeight scrollHeight offsetTop scrollTop 这5个和元素高度.滚动.位置相关的属性,单凭单词很难搞清楚分 ...

  3. 四种浏览器对 clientHeight、offsetHeight、scrollHeight、clientWidth、offsetWidth 和 scrollWidth 的解释差异

    网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...

  4. 详解clientHeight、offsetHeight、scrollHeight

    关于clientHeight.offsetHeight.scrollHeight   window.screen.availWidth 返回当前屏幕宽度(空白空间)  window.screen.av ...

  5. height、clientHeight、offsetHeight、scrollHeight、height()、 innerHeight()、outerHeight()等的区别

    1.height height是css属性,这个属性定义元素内容区的高度,在内容区外面可以增加内边距.边框和外边距. 当  box-sizing: content-box 时,高度应用到元素的内容框. ...

  6. clientHeight ,offsetHeight,style.height,scrollHeight有区别与联系

    style.height 包括 元素的滚动条,不包括边框clientHeight 不包括元素的滚动条和边框,只有在没有元素的滚动条的情况下,style.height相等于clientHeightoff ...

  7. 浅谈style.height、clientHeight、offsetHeight、scrollHeight

    先分别介绍以下,以下资料来自MDN HTMLElement.offsetHeight 是一个只读属性,它返回该元素的像素高度,高度包含该元素的垂直内边距和边框,且是一个整数. Element.clie ...

  8. 一起看看 scrollHeight,clientHeight,offsetHeight,scrollTop是个啥

    scrollHeight最终数值的组成: var scrollHeight = currentElementContent.height +currentElement.paddingTop+curr ...

  9. clientHeight & offsetHeight & scrollHeight

    clientHeight & offsetHeight & scrollHeight scrollWidth/scrollHeight,offsetWidth/offsetHeight ...

随机推荐

  1. 测试table数据 winfrom datagridview 点击标头数字排序的时候table 列类型要为数字类型

    public DataTable GenerateData(int NoOfRecord){DataTable tbl = new DataTable();tbl.Columns.Add(new Da ...

  2. JSF 抽象和实现例子 (函数和属性)

    ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/ ...

  3. Correspondence / ˏkɔris'pɔndәns / dictionary10-800.doc

    I have taken courses in office administration, typing,reports and correspondence writing. Correspond ...

  4. Linux命令(2)- mv

    mv 功能:可以用来移动文件或者将文件改名. 格式:mv [选项] 源文件或目录 目标文件或目录 说明:mv命令将文件重命名或将其移至一个新的目录中.第二个参数类型是文件时,mv命令完成文件重命名,此 ...

  5. flex 正则 验证

    验证数字:^[-]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d{m,n}$ 验证零和非零开头的数字:^(|[-][-]*)$ 验证有两位小数的正 ...

  6. SQL Server 查看物理页存储

    创建测试表 Use Test create table dbo.employee( emp_lname varchar(12) not null, emp_fname varchar(12)not n ...

  7. PHP static关键字

    声明类成员或方法为static,就可以不实例化类而直接访问.不能通过一个对象来访问其中的静态成员(静态方法除外). 为了兼容PHP4,如果没有指定“可见性”,属性和方法默认为public. 由于静态方 ...

  8. Hibernate个人学习笔记(1)

    连接池c3p0所需jar包:Hiberbate开发包-lib-optional-c3p0下全部Jar包 Hiberbate连接池参数配置:Hiberbate开发包-project-etc-hibern ...

  9. Training Deep Neural Networks

    http://handong1587.github.io/deep_learning/2015/10/09/training-dnn.html  //转载于 Training Deep Neural ...

  10. window 2003 配置FTP +防火墙设置

    2保险的做法是 不允许匿名登录,吧钩去掉 后面我们会添加一个用户,并且赋予权限 3 主目录 可以设置时当前计算机目录或者是另一台计算机目录(映射) FTP站点目录:浏览定位FTP文件所在站点,给予是否 ...