Element属性:scrollHeight,clientHeight,offsetHeight区别

相关属性解释见专栏其他文章介绍

只讨论块级元素的情况下:

(1)元素没有滚动条,且没有border-top, border-bottom时,三者相等

(2)元素没有垂直滚动条时,scrollHeight,clientHeight相等

(3)元素有垂直滚动条,且没有border-top, border-bottom时,clientHeight和offsetHeight相等

(4)元素有垂直滚动条,且有border-top或 border-bottom时,三者不相等

一般获取元素高度用offsetHeight,涉及到元素滚动相关的场景则会用到scrollHeight和clientHeight。

JS在线编辑器,JS在线运行,在线演示,调试测试代码

​<style>
    * {
        margin: 0;
        box-sizing: border-box;
    }
    #father1 {
        width: 150px;
        height: 150px;
        padding: 10px;
        background: red;
        overflow: auto;
    }
    #son1 {
        width: 50px;
        height: 50px;
        background: blue; 
        line-height: 50px;
        text-align: center;
        color: #fff;
    }
    #father2 {
        margin-top: 10px;
        width: 150px;
        height: 150px;
        padding: 10px;
        border: 5px solid green;
        background: red;
        overflow: auto;
    }
    #son2 {
        width: 250px;
        height: 50px;
        background: blue; 
        line-height: 50px;
        text-align: center;
        color: #fff;
    }
    #father3 {
        margin-top: 10px;
        width: 150px;
        height: 150px;
        padding: 10px;
        background: red;
        overflow: auto;
    }
    #son3 {
        width: 50px;
        height: 250px;
        background: blue; 
        line-height: 50px;
        text-align: center;
        color: #fff;
    }
    #father4 {
        margin-top: 10px;
        width: 150px;
        height: 150px;
        padding: 10px;
        border: 5px solid green;
        background: red;
        overflow: auto;
    }
    #son4 {
        width: 50px;
        height: 250px;
        background: blue; 
        line-height: 50px;
        text-align: center;
        color: #fff;
    }
</style>
<body>
    <div id="father1">
        <div id="son1">
             son1
        </div>
    </div>
    <div id="father2">
        <div id="son2">
             son2
        </div>
    </div>
    <div id="father3">
        <div id="son3">
             son3
        </div>
    </div>
    <div id="father4">
        <div id="son4">
             son4
        </div>
    </div>
</body>
<script>
  let father1 = document.getElementById("father1");
  // 元素没有滚动条,且没有border-top, border-bottom时,三者相等
  console.log(father1.scrollHeight, father1.clientHeight, father1.offsetHeight) // 150,150, 150
  let father2 = document.getElementById("father2");
  // 元素没有垂直滚动条时,scrollHeight,clientHeight相等
  console.log(father2.scrollHeight, father2.clientHeight, father2.offsetHeight) // 133,133, 150
  let father3 = document.getElementById("father3");
  // 元素有垂直滚动条,且没有border-top, border-bottom时,clientHeight和offsetHeight相等
  console.log(father3.scrollHeight, father3.clientHeight, father3.offsetHeight) // 270,150,150
  let father4 = document.getElementById("father4");
  // 元素有垂直滚动条,且有border-top或 border-bottom时,三者不相等
  console.log(father4.scrollHeight, father4.clientHeight, father4.offsetHeight) // 270,150,150
</script> 

​
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值