一、概念
clientHeight = content + padding, 不加溢出内容的高度 ,返回整数
offsetHeight = content + padding+border, 不加溢出内容的高度 ,返回整数
scrollHeight = content + padding+溢出内容的高度,不加border,返回整数
注意:当内部子元素高度小于等于当前元素时,clientHeight 和 secrollHeight是相等的,因为没有内容溢出。
二、实战
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
html, body{
margin: 0;
padding: 0;
}
.outer-box{
background-color: red;
height: 100px;
padding: 10px;
overflow: auto;
border: 100px green solid;
}
.inner-box{
background-color: black;
height: 200px;
}
</style>
</head>
<body>
<div class="outer-box">
<div class="inner-box">
</div>
</div>
<script>
let box = document.querySelector('.outer-box');
console.log('clientHeight: ' + box.clientHeight);
console.log('offsetheight: ' + box.offsetHeight);
console.log('scrollHeight: ' + box.scrollHeight);
</script>
</body>
</html>