Skip to content

Instantly share code, notes, and snippets.

@niksumeiko
Created July 14, 2013 09:03
Show Gist options
  • Save niksumeiko/5993688 to your computer and use it in GitHub Desktop.
Save niksumeiko/5993688 to your computer and use it in GitHub Desktop.
jQuery function that identifies page scrolling direction. Used when it's needed to apply different functionality for different (up/down) scrolling directions.
// Variable that is going to hold previous 'document' scrollTop
// value (/vertical scrollbars position).
var prevScrollTop;
// Function that returns 'true' (/boolen) if user scrolls the
// page up, 'false' (/boolen) if user scrolls the page down.
function scrollsUp(scrollTop) {
var before = prevScrollTop;
// Storing current 'document' scrollTop value.
prevScrollTop = scrollTop;
return scrollTop < before;
}
// Listening to 'window' scroll event.
$(window).on('scroll', function() {
// Getting current 'document' scrollTop value.
var scrollTop = $(document).scrollTop();
if (scrollsUp(scrollTop)) {
// User scrolls the page up.
} else {
// Otherwise, user scrolls the page down.
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment