javascript - `document.documentElement` 的 offsetHeight 行为不同
问题描述
我知道offsetHeight
给我元素的 CSS 高度的概念,包括边框和填充以及滚动条宽度/高度(如果存在)并且当我在我的 html 中有元素时完美工作,即
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style>
#content {
width: 400px;
height: 100px;
overflow-y: scroll;
border: 2px solid black;
}
</style>
</head>
<body>
<div id="content">
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
</div>
</body>
<script>
const $div = document.getElementById('content');
console.log("CLIENT HEIGHT ", $div.clientHeight);
console.log("SCROLL HEIGHT ", $div.scrollHeight);
console.log("OFFSET HEIGHT ", $div.offsetHeight);
</script>
</html>
输出 :
"CLIENT HEIGHT "
100
"SCROLL HEIGHT "
234
"OFFSET HEIGHT "
104
但是,当我打开浏览器控制台并做同样的事情时,document.documentElement
我得到了这个:
你可以看到offsetHeight
它也包括内容,并且类似于scrollHeight
莫名其妙!如果你请纠正我哪里出错了,我很困惑......谢谢!:D
解决方案
推荐阅读
- linker-errors - libtool 过度复制对象以解决名称冲突
- javascript - 导航栏切换按钮在使用 Bootstrap 4.5.3 时不起作用
- python - 代数金字塔python挑战-更有效的方法?
- python - Python 扩展 dict() 以添加静态元素
- node.js - 当我注销时,我无法使用相同的电子邮件和密码登录,显示错误 jwt
- azure - 如何使用 json/csv 文件中的数据创建 Azure DevOps 库变量组?
- python - 添加属性和值 selenium python
- dependency-injection - 依赖注入容器返回对局部函数中分配的值的引用在 Rust 中根本不可能吗?
- aws-lambda - 如何限制无服务器 lambda 函数的并行执行
- android - 带有正/负和复选框的 Android 对话框