Let's say that I have a div $container
with position: relative
, and a div $foo
within it with position: absolute
. I want to get the position of $foo
relative to the container. jQuery makes this easy:
$foo.position()
which returns an object with top
and left
properties, specified in pixels. The problem is, this number is no longer accurate if the container is scrolled down or to the right at all. Specifically, it changes, when it shouldn't. jQuery computes position()
such that it's equivalent to $foo.offset() - $container.offset()
(where offset()
gives the window-relative position), and $foo.offset()
changes as the user scrolls around $container
.