Is there a predictable relationship between cummulative offsetTop of an <a> element and the value of scrollTop after navigating to that <a> element?
I had naively assumed they would be equal, but I've encountered cases where scrollTop is larger (the scroll position is further down the page).
The following defines the question more precisely:
function TotalOffsetTop (e)
{
var offset = 0;
do
offset += e.offsetTop;
while (e = e.offsetParent);
return offset;
}
//navigate to MyBookmark
location.hash = "#MyBookmark"
var BookmarkOffsetTop = TotalOffsetTop(document.getElementByID("MyBookmark"));
var CurrentPosition = document.getElementsByTagName("body")[0].scrollTop;
if ( CurrentPosition != BookmarkOffsetTop ) alert("Design Flaw!");
My ultimate goal is to find the nearest <a> tag whose start is above the top of the current viewport. This is so I can jump backward (and forward) to bookmarks relative to the current position in a document.
Are there some other measures I should be using instead of scrollTop and cummulative offsetTop?
I'm exploring the problem using Chrome, but I'd like to ultimately find something that works in at least several modern browsers. I'm trying to avoid jQuery.