简体   繁体   English

元素宽度/高度的方法

[英]methods for element width/height

Question: Is there a scenario where getBoundingClientRect and window.getComputedStyle would differ in width or height ? 问题:是否存在getBoundingClientRectwindow.getComputedStyle widthheight不同的情况?

I just found a inconsistent width (see under) in IE when a element has box-sizing where window.getComputedStyle returns the wrong value. 我刚刚在IE中找到了一个不一致的width (见下文),当一个元素有box-sizingwindow.getComputedStyle返回错误的值。

So I thought about overriding just width and height with values from getBoundingClientRect but not sure if there are cases where that would fail. 所以我考虑用getBoundingClientRect值覆盖widthheight ,但不确定是否存在失败的情况。

Example of the problem (broken in IE): http://jsfiddle.net/bwPM8/ 问题的例子(在IE中被破坏): http//jsfiddle.net/bwPM8/

var box = document.querySelector('.box');
var gBCR_width = box.getBoundingClientRect().width; // always 200
var wGCS = window.getComputedStyle(box).width; // 200 some browsers, 160 in IE

In IE the CSS padding: 10px; 在IE中,CSS padding: 10px; cause overflow and that gives you extra size of the computedStyle.. 导致溢出,这给你额外的computedStyle大小..

Also IE calculates Borders separately from the object. IE Borders与对象分开计算Borders

This sums up the answer with difference of 40px 这总结了40px差异的答案

Now with overflow:hidden; 现在有overflow:hidden; OR box-sizing: border-box; box-sizing: border-box; that cause value go in minus so will become 200px - 40px = 160px. 导致值变为负值因此将变为200px - 40px = 160px。

Note: Here if we remove overflow:hidden will not make any difference as the box-sizing:border-box cause the design not to grow further from defined height and width. 注意:这里如果我们删除overflow:hidden不会有任何区别,因为box-sizing:border-box导致设计不会从定义的高度和宽度进一步增长。

I have generate another fiddle 1 (without padding) which gives 我生成另一个fiddle 1 (没有填充)给出

'computedValue : 180px' 'computedValue:180px'

And with Border:0px the fiddle 2 give results same as in other browser.. 并且使用Border:0pxfiddle 2给出与其他浏览器相同的结果。

I hope this clears what causes what in IE(IE has its own mind that sometimes cause pain to developers) 我希望这能清除导致IE中的内容(IE有自己的想法,有时会给开发人员带来痛苦)

Yes, there are several differences between these two functions. 是的,这两个功能之间存在一些差异。

getBoundingClientRect() will return a text rectangle object that encloses a group of text rectangles (the union of the rectangles returned by getClientRects() for the element, ie, the CSS border-boxes associated with the element). getBoundingClientRect()将返回一个文本矩形对象,该对象包含一组文本矩形(由元素的getClientRects()返回的矩形的并集,即与元素关联的CSS边框)。 The latter, getComputedStyle() , will return the computed CSS style of that element, after applying all CSS. 后者getComputedStyle()将在应用所有CSS后返回该元素的计算CSS样式。

Therefore the resulting width and height can be drastically different. 因此,所得到的宽度和高度可以完全不同。

For instance, by simply applying a transform to that element you already change the width that is returned by getBoundingClientRect() : 例如,通过简单地将transform应用于该元素,您已经更改了getBoundingClientRect()返回的宽度:

http://jsfiddle.net/epW3c/ http://jsfiddle.net/epW3c/

Moreover, even if you add a border or padding to your element, the width and height will be different in both cases. 此外,即使您为元素添加边框或填充,两种情况下的宽度和高度也会不同。

Depending on the version of IE you're testing this on, you might be experiencing the infamous Internet Explorer box model bug which causes the interpretation of the dimensions of the element to be wrong. 根据您正在测试的IE版本,您可能会遇到臭名昭着的Internet Explorer盒模型错误 ,导致元素维度的解释错误。 Make sure you're not running your page in quirks mode by adding the doctype properly. 通过正确添加doctype,确保您没有在怪癖模式下运行页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM