首页 > 解决方案 > scrollHeight 会永远小于 clientHeight 或 offsetHeight 吗?

问题描述

如果我写Math.max(el.scrollHeight, el.offsetHeight, el.clientHeight),这总是相等el.scrollHeight吗?

一般来说,clientHeight <= offsetHeight <= scrollHeight. clientHeight > offsetHeight元素的情况很少见html,但我不确定是否scrollHeight可以小于其他两个。

标签: javascriptdom

解决方案


offsetHeight 属性包括元素的边框,因此绝对可以大于 scrollHeight。例如

let test = document.getElementById('test');
console.log("clientHeight = " + test.clientHeight);
console.log("offsetHeight = " + test.offsetHeight);
console.log("scrollHeight = " + test.scrollHeight);
console.log("Max Height = " 
    + Math.max(test.scrollHeight, test.offsetHeight, test.clientHeight));
#test {
  height:20px;
  border:0px solid red;
  border-block-width: 20px;
  background-color:blue;
}
<div id="test"></div>


推荐阅读