You can use element.getBoundingClientRect() to retrieve the position of an element relative to the viewport.
Use scrollY ( document.documentElement.scrollTop cross-browser compatible) to calculate viewport offset.
The sum of these two values will contain the position of the element relative to the document:
subjectRect.top + document.documentElement.scrollTop
Dmitry_F Sep 07 '13 at 13:11 2013-09-07 13:11
source share