CSSOM之getboundingclientrect和getclientrects

來源:互聯網
上載者:User

標籤:換行   擷取   支援   bsp   isp   rectangle   css   相對   image   

TextRectangle

對於文字物件,W3C提供了一個 TextRectangle 對象,這個對象是對文本地區的一個解釋。

對於 i,span,em等display 是inline的標籤,在書寫文本的時候可能存在換行,像:

每個紅色的框,就是一個TextRectangle對象。

使用 getClientRects() 方法可以得到一個TextRectangle 的list。 每個對象代表一個紅框

如果使用 getBoundingClientRect(). 則會得到一個包含這4個紅框的一個最小矩形,當然也是一個TextRectangle對象。

如果把em display設定成inlineblock,那麼 無論調用者兩個方法中的哪一個,都只返回一個TextRectangle對象。

瀏覽器差異

除了safari,firefox2.0外所有瀏覽器都支援getClientRects和getBoundingClientRect,
firefox 3.1給TextRectangle增加了 width 和 height。

ie 和非ie瀏覽器在使用getClientRects還是有些差別的,ie擷取TextRectangleList的範圍很大。而非ie擷取的範圍比較小, 只有display:inline的對象才能擷取到TextRectangleList,例如em i span 等標籤。

使用情境

現 在用得最多的是getBoundingClientRect,這個直接返回一個TextRectangle,其實大家經常使用它來擷取到一個 element的viewport座標.其實就算dom裡面沒有文本也能返回一個 TextRectangle. 這樣就不需要向上遍曆來計算對象的相對座標了。

CSSOM之getboundingclientrect和getclientrects

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.