Is there a method for getting the client rect for an element relative to the document besides offset? getBoundingClientRect()
gets the value relative to the client's browser.
I'm using D3
and jQuery's height()
nor width()
are working (I've even tried doing it window.load()
), but offset()
is. Neither are javascripts .offset
return [$e.offset().top + $e.height()/2, $e.offset().left + $e.width()/2]
$e.height()
and $e.width()
both return 0
It's an SVG element, I'm just using it to edit my SVG. It's much easier to load/handle SVG's with D3
. The project is not data related, it's just a map.
HTML DOM getBoundingClientRect () Method returns the relative positioning to the viewport. It returns eight properties: left, top, right, bottom, x, y, width, height. Scrolling will change the position value.
The returned object is assigned to variable Rect. We then use the left, top, width and height properties of the DOMRect object to display its position and size relative to viewport. This information is displayed in the paragraph with id “Sample” using its innerHTML property −
If all the element's border-boxes are empty, then a rectangle is returned with a widthand heightof zero and where the topand leftare the top-left of the border-box for the first CSS box (in content order) for the element.
Using element.getBoundingClientRect()
by itself returns top
and left
values that are relative to the viewport, like you discovered. If you want them to be relative to the document (and not be affected by scroll position), you can add the scroll position using window.scrollY
and window.scrollX
, like so:
const rect = element.getBoundingClientRect()
rect.left // (relative to viewport)
rect.top // (relative to viewport)
rect.left + window.scrollX // (relative to document)
rect.top + window.scrollY // (relative to document)
Source.
Here's an ES6 method that returns all the same properties as getBoundingClientRect()
but relative to the entire document.
const getOffsetRect = (el) =>
let rect = el.getBoundingClientRect();
// add window scroll position to get the offset position
let left = rect.left + window.scrollX;
let top = rect.top + window.scrollY;
let right = rect.right + window.scrollX;
let bottom = rect.bottom + window.scrollY;
// polyfill missing 'x' and 'y' rect properties not returned
// from getBoundingClientRect() by older browsers
let x = rect.x === undefined ? left : rect.x + window.scrollX;
let y = rect.y === undefined ? top : rect.y + window.scrollY;
// width and height are the same
let width = rect.width;
let height = rect.height;
return { left, top, right, bottom, x, y, width, height };
};
Note: it's also poly-filling the x
and y
props, that are not returned from getBoundingClientRect() by older browsers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With