關於瀏覽器的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文檔任何東西。)
這是一個錯誤或功能?
是的,這是一個錯字。謝謝 – 2014-11-17 15:19:31
Merci,@Martin。 – Parapluie 2014-11-18 16:17:55