2013-05-01 117 views
6

关于浏览器的JavaScript,所述window.getComputedStyle()方法应该给施加到元件的CSS属性的最终使用的值。根据MDN documentation,这意味着“所有计算已经完成”。为什么getComputedStyle不考虑边缘折叠?

然而,似乎“所有的计算”不包括保证金崩溃。在Firefox和Chrome(至少)中,指令getComptedStyle().marginBottom在计算任何边距折叠之前返回计算值。

例如,请考虑以下因素:

<div style="margin: 10px 0 15px 0"></div> 

其顶部和底部空间将崩溃,因为(大约)其内容高度为零(参见the W3C CSS2 Recommendation)。该CSSOM方法将返回这些值:

getComputedStyle().marginTop → 10px 
getComputedStyle().marginBottom → 15px 
getBoundingClientRect().top → {top of margin box} + marginTop 
getBoundingClientRect().bottom → idem top 

但是,由于余量折叠,所述布局显示10px的的边界矩形的客户端之前的余量,和5像素的后边际,即max(0, marginBottom - marginTop)

为什么不getComputedStyle().marginBottom回报直接为5px,真正使用的值“所有的计算后,已执行”,而不是指定15px的?这是W3C推荐的行为吗? (我还没有看到有关此内容的w3.org文档任何东西。)

这是一个错误或功能?

回答

1

我没有看到所有的代码,但我认为,函数名实际上是“的getComputedStyle,”一个“U”。

一个错字?这会容易吗?你不会是第一个 - 我自己包括在内。

希望这有助于。

+0

是的,这是一个错字。谢谢 – 2014-11-17 15:19:31

+0

Merci,@Martin。 – Parapluie 2014-11-18 16:17:55