获取及计算可滚动元素高度:区分clientHeight、offsetHeight、scrollHeight、offsetTop、scrollTop

获取元素高度

一、基础:理清clientHeight、offsetHeight、scrollHeight、offsetTop、scrollTop区别

图文可见:https://blog.csdn.net/qq_35430000/article/details/80277587

说的非常详细,这边列表总结如下:

名称范围
clientHeightcontent高度(即包括上下padding)
offsetHeightcontent高度+上下border
scrollHeight元素可滚动时,content实际高度;因此scrollHeight恒>=offsetHeight
offsetTop在有滚动条时,滚动条向下滚动的距离也就是元素顶部被遮住部分的高度,无滚动条时为0
scrollTop当前元素顶部距离最近父元素顶部的距离,和有没有滚动条无关。
如果要获取margin值怎么取?
// ele 表示指定元素dom节点
const marginBt = parseFloat(window.getComputedStyle(ele).marginBottom)

二、获取滚动元素高度

(以vue为例)

<div $ref="listheight">
......
</div>

const listheight = this.$refs.listheight.scrollHeight

三、手动计算指定子元素高度

let chil = this.$refs.listheight.children;
  // 可在for循环中获取任意的子元素,此处为所有元素的offsetHeight以及marginBottom值
  let calculateHeight = 0;
  for(var i = chil.length - 1; i >= 0; i--) {
    calculateHeight += chil[i].offsetHeight + parseFloat(window.getComputedStyle(chil[i]).marginBottom);
  }

参考:
https://blog.csdn.net/qq_35430000/article/details/80277587

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值