如何获得两个div之间的距离

NoN*_*meZ 5 javascript jquery jquery-mobile

所以我在另一个里面有一个div - 我怎么能在它们之间找到距离?

纽扣

我尝试了类似的东西$('#child').parentsUntil($('#parent')).andSelf()- 但它返回一个物体,而不是距离.

PS我需要它来推其他按钮.

Rok*_*jan 5

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

要获得左距离,您可以使用:

var distLeft = $('#child').position().left; 
Run Code Online (Sandbox Code Playgroud)

这将返回px相对于偏移父级的距离

如果您对元素的页面偏移感兴趣,则:

var offsLeft = $('#child').offset().left;
Run Code Online (Sandbox Code Playgroud)

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