Getting the current scroll position in Chrome using scrollTop() on page load/refresh
The scrollTop()
returns the current vertical position of the scroll bar. Typically on page load, the scroll bar is at position 0
. If the console prints out something else, then you or the browser must have scrolled down before the function was called.
If you are using named anchors or refreshing the page from a scrolled position, you can bind a handler to the scroll event that only triggers once - on page load:
$(window).on('scroll', function() {
console.log( $(this).scrollTop() );
});