获取元素的高度减去填充,边距,边框宽度

Joe*_*Joe 42 javascript css

当没有内联高度声明时,是否有人知道是否可以获得元素的高度(减去垂直填充,边框和边距)?我需要支持IE8及以上版本.

el.style.height 不起作用,因为样式是在外部样式表中设置的.

el.offsetHeight或者el.clientHeight不起作用,因为它们不仅仅包含元素的高度.而且我不能只减去元素的填充等,因为这些值也是在CSS样式表中设置的,而不是内联的(因此el.style.paddingTop不起作用).

也做不到window.getComputedStyle(el)因为IE8不支持这个.

jQuery有height()方法,它提供了这个,但是我没有在这个项目中使用jQuery,而且我只是想知道如何在纯JavaScript中执行此操作.

有人有什么想法?非常感激.

Dan*_*Dan 55

这是适用于以下两种情况的解决方案box-sizing:content-boxborder-box.

var computedStyle = getComputedStyle(element);

elementHeight = element.clientHeight;  // height with padding
elementWidth = element.clientWidth;   // width with padding

elementHeight -= parseFloat(computedStyle.paddingTop) + parseFloat(computedStyle.paddingBottom);
elementWidth -= parseFloat(computedStyle.paddingLeft) + parseFloat(computedStyle.paddingRight);
Run Code Online (Sandbox Code Playgroud)

适用于IE9 +

您可以使用特征检测

if (!getComputedStyle) { alert('Not supported'); } 
Run Code Online (Sandbox Code Playgroud)

如果元素display是,这将不起作用inline.使用inline-block或使用getBoundingClientRect.


Sal*_*bas 14

改进了Dan的代码以处理内联元素(使用offset*而不是client*):

var cs = getComputedStyle(element);

var paddingX = parseFloat(cs.paddingLeft) + parseFloat(cs.paddingRight);
var paddingY = parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom);

var borderX = parseFloat(cs.borderLeftWidth) + parseFloat(cs.borderRightWidth);
var borderY = parseFloat(cs.borderTopWidth) + parseFloat(cs.borderBottomWidth);

// Element width and height minus padding and border
elementWidth = element.offsetWidth - paddingX - borderX;
elementHeight = element.offsetHeight - paddingY - borderY;
Run Code Online (Sandbox Code Playgroud)

  • 注意offsetHeight也包括scrollBar的大小!因此,如果所需的行为是不包括不会做的scrollBar。计算滚动宽度/sf/answers/936801141/, (2认同)

Bho*_*yar 9

干得好:

var style = window.getComputedStyle(document.getElementById("Example"), null);
style.getPropertyValue("height");
Run Code Online (Sandbox Code Playgroud)

以上版本适用于现代浏览器.请检查IE浏览器的currentStyle.

跨浏览器:

try {
 el = window.getComputedStyle(document.getElementById('example'), null)
     .getPropertyValue('height');
} catch(e) {
 el = document.getElementById('example').currentStyle.height;
} 
Run Code Online (Sandbox Code Playgroud)

资源

  • 如果你使用`box-sizing:border-box`,这将是最常用的. (26认同)
  • 这包含填充,至少当我试图在SVG上使用它时 (5认同)
  • 在这种情况下,没有必要使用`try ... catch`,我们可以使用`in`运算符来检查成员是否存在,例如通过`if('getComputedStyle'在窗口中)...`或者使用`typeof`,如`if(typeof window.getComputedStyle!=='undefined')...` (3认同)
  • 我要补充的一件事是,如果元素的高度是auto,`.currentStyle.height`将只返回'auto',如果它的高度是百分比,它将只返回该百分比,而不是像素的计算高度.所以在这种情况下,如果您仍然需要知道元素的高度,可以使用它的`.offsetHeight`,然后使用`.currentStyle`获取任何垂直填充或边框值,并从offsetHeight值中减去它们给你元素的高度.如果填充或边框也设置为百分比,则这不起作用. (2认同)

Jam*_*ton 8

把丹的答案变成了一个函数

export const innerDimensions = (node) => {
  var computedStyle = getComputedStyle(node)

  let width = node.clientWidth // width with padding
  let height = node.clientHeight // height with padding

  height -= parseFloat(computedStyle.paddingTop) + parseFloat(computedStyle.paddingBottom)
  width -= parseFloat(computedStyle.paddingLeft) + parseFloat(computedStyle.paddingRight)
  return { height, width }
}
Run Code Online (Sandbox Code Playgroud)