Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetBoundingClientRect but relative to the entire document

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.

like image 501
John Avatar asked Jun 05 '13 20:06

John


People also ask

What is getboundingclientrect in HTML?

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.

How to display the position of the returned object relative to viewport?

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 −

How to return a rectangle when border-boxes are empty?

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.


2 Answers

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.

like image 114
Ian Storm Taylor Avatar answered Oct 27 '22 03:10

Ian Storm Taylor


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

like image 22
Lucidity Avatar answered Oct 27 '22 02:10

Lucidity