Created
July 14, 2013 09:03
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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