Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get bounding box for div element in jQuery

I want to calculate bounding box for div element via jQuery/JavaScript.

I tried like this:

//Left side of box  document.getElementById("myElement").offsetLeft;  //Top side of box  document.getElementById("myElement").offsetTop;   //Right side of box document.getElementById("myElement").offsetLeft + document.getElementById("myElement").offsetWidth;  //Bottom side of box  document.getElementById("myElement").offsetTop + document.getElementById("myElement").offsetHeight;  

It returns some values. whether it is correct way to get bounding box for div element via jQuery / JavaScript.

I need something like getBBox() method in SVG element. it will return x, y, width and height of an element. Sameway how can I get bounding box for div element?

like image 659
SivaRajini Avatar asked Sep 13 '13 06:09

SivaRajini


People also ask

What is getBoundingClientRect () in Javascript?

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.

What is '$' in jQuery?

$ is pretty commonly used as a selector function in JS. In jQuery the $ function does much more than select things though. You can pass it a selector to get a collection of matching elements from the DOM. You can pass it a function to run when the document is ready (similar to body.

What is getBoundingClientRect () Top?

The Element.getBoundingClientRect() method returns a DOMRect object providing information about the size of an element and its position relative to the viewport.

Does getBoundingClientRect include margin?

Border, padding and margin are not included. This means when you set a width, That width is set to the content only then you add the padding and border.


2 Answers

As this is specifically tagged for jQuery -

$("#myElement")[0].getBoundingClientRect(); 

or

$("#myElement").get(0).getBoundingClientRect(); 

(These are functionally identical, in some older browsers .get() was slightly faster)

Note that if you try to get the values via jQuery calls then it will not take into account any css transform values, which can give unexpected results...

Note 2: In jQuery 3.0 it has changed to using the proper getBoundingClientRect() calls for its own dimension calls (see the jQuery Core 3.0 Upgrade Guide) - which means that the other jQuery answers will finally always be correct - but only when using the new jQuery version - hence why it's called a breaking change...

like image 81
Rycochet Avatar answered Oct 08 '22 22:10

Rycochet


You can get the bounding box of any element by calling getBoundingClientRect

var rect = document.getElementById("myElement").getBoundingClientRect(); 

That will return an object with left, top, width and height fields.

like image 35
Robert Longson Avatar answered Oct 08 '22 22:10

Robert Longson