0

I am currently using the following JavaScript function to detect whether an element is scrolled into view:

function isScrolledIntoView(elem) {
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();
    var elemTop = $(elem).offset().top;
    return ((elemTop <= docViewBottom) && (elemTop >= docViewTop));
}

Now, I need to distinguish whether this element was autoscrolled on page load (e.g. via #anchor-tag) or manually scrolled into view.

What is a good way of doing this?

Allen Liu
  • 3,948
  • 8
  • 35
  • 47

1 Answers1

0

When an anchor is clicked, the URL hash changes. You can use window.onhashchange to detect that:

window.onhashchange = function() {
    console.log(window.location.hash);
}
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • True but this solution will not work in my case because all the URLs will have the # as part of the URL. – Allen Liu Oct 08 '13 at 22:28