Return the size of an element and its position relative to the viewport:
const rect = element.getBoundingClientRect();
Try it Yourself »
The getBoundingClientRect()
method returns the size of an element and its position relative to the viewport.
The getBoundingClientRect()
method returns a DOMRect object with eight properties: left, top, right, bottom, x, y, width, height.
The scrolling that has been done is taken into account. This means that the rectangle's edges (top, left, bottom, and right) change their values every time the scrolling position changes.
element.getBoundingClientRect()
Type | Description |
Object | A DOMRect object with eight properties: left, top, right, bottom, x, y, width, height. |
element.getBoundingClientRect()
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!