I'm trying to create a jQuery snippet that enables me to add 4 different classes to a div based on the user's current scroll position.
The desired states are: 1. User is at top of page 2. User is at bottom of page 3. User is scrolling down 4. User is scrolling up
In the below code I have achieved number 1 & 2, but am not able to get 3 & 4 to work.
Can anybody help? Thanks.
// Add/remove classes to navigation based on position
var scrollPosition = $(window).scrollTop();
$(window).bind( 'load scroll', function(){
// User is at bottom of window
if($(window).scrollTop() + $(window).height() === $(document).height()) {
$('#global-header').removeClass().addClass('at-bottom');
// User is at top of window
} else if ($(window).scrollTop() === 0) {
$('#global-header').removeClass().addClass('at-top');
} else {
$('#global-header').removeClass();
}
});