如何使用 window.getComputedStyle() 获得全高(包括边距)?

我正在尝试使用 JSX element 获得 ReactJSwindow.getcomputedStyle 的完整高度,但是我想获得完整值 height+margin,但我找不到方法。

当我只使用高度时,我没有得到边距值。

window.getcomputedStyle(productsContainerRef.current).height

我知道我可以分别获得两个值并将它们相加,但是有没有一种直接的方法来做到这一点?

我使用 ref 获取元素,因此,不可能使用事件瞄准目标元素并获取 outerHeight

有没有办法使用 getcomputedStyle 做到这一点?

tanxueli 回答:如何使用 window.getComputedStyle() 获得全高(包括边距)?

一种选择是将边距添加到 refs 当前高度。

示例:

  const outerHeight = (el) => {
    var height = el.offsetHeight;
    var style = window.getComputedStyle(el);
    height += parseInt(style.marginTop) + parseInt(style.marginBottom);
    return height;
  };
本文链接:https://www.f2er.com/19968.html

大家都在问