Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Element.getBoundingClientRect()

Esta tradução está incompleta. Ajude atraduzir este artigo.

O método Element.getBoundingClientRect() retorna o tamanho de um elemento e sua posição relativa ao ponto de vista.

Syntax

rectObject = object.getBoundingClientRect();

Return value

The returned value is a DOMRect object which is the union of the rectangles returned by getClientRects() for the element, i.e., the CSS border-boxes associated with the element.

The returned value is a DOMRect object, which contains read-only left, top, right and bottom properties describing the border-box in pixels. top and left are relative to the top-left of the viewport.

Note: Gecko 1.9.1 adds width and height properties to the DOMRect object.

Empty border-boxes are completely ignored. If all the element's border-boxes are empty, then a rectangle is returned with a width and height of zero and where the top and left are the top-left of the border-box for the first CSS box (in content order) for the element.

The amount of scrolling that has been done of the viewport area (or any other scrollable element) is taken into account when computing the bounding rectangle. This means that the top and left property change their values as soon as the scrolling position changes (so their values are relative to the viewport and not absolute). If this is not the desired behaviour just add the current scrolling position to the top and left property (via window.scrollX and window.scrollY) to get constant values independent from the current scrolling position.

Scripts requiring high cross-browser compatibility can use window.pageXOffset and window.pageYOffset instead of window.scrollX and window.scrollY. Scripts without access to window.pageXOffset, window.pageYOffset, window.scrollX and window.scrollY can use:

// For scrollX
(((t = document.documentElement) || (t = document.body.parentNode))
  && typeof t.ScrollLeft == 'number' ? t : document.body).ScrollLeft
// For scrollY
(((t = document.documentElement) || (t = document.body.parentNode))
  && typeof t.ScrollTop == 'number' ? t : document.body).ScrollTop

Example

// rect is a DOMRect object with six properties: left, top, right, bottom, width, height
var rect = obj.getBoundingClientRect();

Specifications

Specification Status Comment
CSS Object Model (CSSOM) View Module
The definition of 'Element.getBoundingClientRect()' in that specification.
Working Draft Initial definition

Notes

getBoundingClientRect() was first introduced in the MS IE DHTML object model.

The return value of getBoundingClientRect() is frozen.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0[1] 3.0 (1.9) 4.0[2] (Yes) 4.0
width/height (Yes) 3.5 (1.9.1) 9 (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.0 1.0 1.0 (1.9) 6.0 (Yes) 4.0

[1] CSS spec for 'use' element referencing 'symbol' element requires default width and height for 'use' element set to 100%. Also spec for width and height 'svg' attributes requires 100% as default values. Google Chrome does not follow these requirements for 'use' element. Also Chrome does not take 'stroke-width' into account. So getBoundingClientRect() may return different rectangles for Chrome and for Firefox.

[2] In IE8 and below, the DOMRect object returned by getBoundingClientRect() lacks height and width properties. Also, additional properties (including height and width) cannot be added onto these DOMRect objects.

Starting from Gecko 12.0 (Firefox 12.0 / Thunderbird 12.0 / SeaMonkey 2.9), the effect of CSS transforms is considered when computing the element's bounding rectangle.

See also

Etiquetas do documento e colaboradores

 Colaboradores desta página: Daibushi
 Última atualização por: Daibushi,