How to get the distance between two divs

So, I have one div inside the other - how can I get the distance between them?

buttons

I tried something like $('#child').parentsUntil($('#parent')).andSelf()- but it returns an object, not a distance.

PS I need him to press other buttons.

+5
source share
4 answers

http://api.jquery.com/position/

to get the left distance you can use:

var distLeft = $('#child').position().left; 

This will return the distance in pxrelation to the offset parent

if you are interested in the page offset of the element than:

var offsLeft = $('#child').offset().left;

http://api.jquery.com/offset/

+4
source

You can use offset

  var childOffset = $('#child').offset(), parentOffset = $('#child').parentsUntil($('#parent')).offset();
    var leftDistance  =childOffset.left - parentOffset.left;
    var topDistance = childOffset.top- parentOffset.top;
+2
source

- ?

 $('innerDiv').position().left; 
0

All Articles