document.documentElement.clientHeight与document.body.clientHeight的区别

网页可见区域高:document.body.clientHeight
网页正文全文高:document.body.scrollHeight
clientHeight: 表示可视区域高度, 包括padding但不包括border、水平滚动条、margin的元素的高度

offsetHeight:表示可视区域高度,包括padding、border、水平滚动条,但不包括margin的元素的高度

scrollHeight: 当元素的子元素高度大于父元素,父元素出现滚动条,此时父元素的clientHeight表示可视的高度,父元素的scrollHeight 表示页面的高度,包括溢出页面被挡住的部分,即其实就是子元素的高度。在没有滚动条时scrollHeight 与clientHeight相等恒成立。单位px,只读元素 。

document.documentElement表示文档的根元素,即html元素

document.body 表示文档中body元素

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    html {
      height: 100%;
    }
    body {
      height: 100%;
    }
   .wrap {
      height: 100%;
      width: 90%;
      background-color: #9a6e3a;
      overflow: auto;
    }
     
    .box {
      height: 2000px;
      width: 50%;
      background-color: red;
    }
  </style>
</head>
<body>
<div class="wrap">
  <div class="box"></div>
</div>
</body>
<script>
  window.onload = function () {
    let $box = document.querySelector('.box')
    let $wrap = document.querySelector('.wrap')

    console.log(document.documentElement.clientHeight)  //屏幕可视区域高度  458
    console.log(document.body.clientHeight)  //屏幕可视区域高度  458

    console.log($wrap.clientHeight)  //屏幕可视区域高度  458

      //子元素高于父元素,内部有滚动条,此时高度包含子元素溢出的部分,即等于子元素高度
    console.log($wrap.scrollHeight)  

    console.log($box.clientHeight)  //子元素内部没有滚动条 等于子元素实际高度
    console.log($box.offsetHeight)  //子元素内部没有滚动条 等于子元素实际高度
  }
</script>
</html>

相关推荐

  1. =====区别

    2024-03-26 23:08:02       35 阅读
  2. & && 区别

    2024-03-26 23:08:02       30 阅读
  3. ajaxaxios区别

    2024-03-26 23:08:02       67 阅读
  4. MFCqt区别

    2024-03-26 23:08:02       56 阅读
  5. TCPUDP区别

    2024-03-26 23:08:02       67 阅读
  6. oraclesqlsever区别

    2024-03-26 23:08:02       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-26 23:08:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-26 23:08:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-26 23:08:02       87 阅读
  4. Python语言-面向对象

    2024-03-26 23:08:02       96 阅读

热门阅读

  1. vue的setup语法糖?

    2024-03-26 23:08:02       39 阅读
  2. 【半结构化访谈法】

    2024-03-26 23:08:02       36 阅读
  3. 24.Python从入门到精通—函数 标准模块 包

    2024-03-26 23:08:02       41 阅读
  4. 【STM32学习计划】

    2024-03-26 23:08:02       38 阅读
  5. 状态机实现单词统计

    2024-03-26 23:08:02       38 阅读
  6. 探索编程语言的发展历程

    2024-03-26 23:08:02       49 阅读
  7. 基于Springboot的个人博客系统的设计与实现

    2024-03-26 23:08:02       30 阅读