js获取元素高度不精确的问题

使用clientHeight获取元素的高度时,会发现获取的都是整数值,其实这是js自动对其进行了四舍五入,这就导致了获取的结果会出现偏差,使用getComputedStyle,就可以解决这个问题。

function getOffsetHeight (element) {
    const styleList = element.currentStyle ? element.currentStyle : window.getComputedStyle(element, null)
    return parseFloat(styleList.height)
}
如果您觉得本文对您有用,欢迎捐赠或留言~
微信支付
支付宝

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注