Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the position of an element relative to a positioned ancestor of its offset parent

I want to determine #target's offset relative to #a in the following HTML document – i.e., I want to find the values of x and y:

http://dl.dropbox.com/u/2792776/screenshots/2012-06-09_1814.png

jQuery has position(), but $("#target").position() returns #target's offset relative to its offset parent, which is #c (not #a)

I need a function that's equivalent to $.fn.position(), but instead returns the position relative to an "offset ancestor" of the target, rather than its direct offset parent. For example: $("#target").positionRelativeTo("#a")

like image 415
Tom Lehman Avatar asked Oct 08 '22 14:10

Tom Lehman


1 Answers

You can calculate it based on offset:

var topoffset = $('#target').offset().top - $('#a').offset().top;
var leftoffset = $('#target').offset().left - $('#a').offset().left;
like image 82
lucuma Avatar answered Oct 11 '22 23:10

lucuma