So I have one div inside the other - how can I get distance between them?
I tried something like $('#child').parentsUntil($('#parent')).andSelf()
- but it returns an object, not a distance.
P.S. I need it to push other buttons.
So I have one div inside the other - how can I get distance between them?
I tried something like $('#child').parentsUntil($('#parent')).andSelf()
- but it returns an object, not a distance.
P.S. I need it to push other buttons.
Share Improve this question edited Oct 1, 2012 at 9:57 NoNameZ asked Oct 1, 2012 at 9:54 NoNameZNoNameZ 7955 gold badges14 silver badges22 bronze badges 1-
Try to test with
$("#child").position().left
and if needed subtract$("#parent").position().left
from this value. – VisioN Commented Oct 1, 2012 at 9:58
4 Answers
Reset to default 5http://api.jquery./position/
to get the left distance you can use:
var distLeft = $('#child').position().left;
That will return the distance in px
relative to the offset parent
if you're interested into the element's page offset than:
var offsLeft = $('#child').offset().left;
http://api.jquery./offset/
There's this awesome getBoundingClientRect
function. Anything else is just a-b
https://developer.mozilla/en-US/docs/DOM/element.getBoundingClientRect
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;
Did you try something like?
$('innerDiv').position().left;