Skip to content

Instantly share code, notes, and snippets.

@Plou
Created September 25, 2013 14:13
Show Gist options
  • Select an option

  • Save Plou/6700228 to your computer and use it in GitHub Desktop.

Select an option

Save Plou/6700228 to your computer and use it in GitHub Desktop.
Detect and fire scrollDown & scrollUp events
(function($) {
// ## Detect and fire scrollDown & scrollUp events
var lastScrollTop = $(window).scrollTop();
$(window).scroll(function(){
var current = $(this).scrollTop();
// Check the direction of the scroll
if (current >= lastScrollTop){
// Trigger the down scroll event
$(this).trigger('jScroll.down');
// Detect a change of direction
if($(this).data('jScroll.direction')) {
// then trigger a scroll direction change event
$(this).trigger('jScroll.direction.change');
}
// set the scroll direction into the jQuery object data
$('body').removeClass('scrollup');
$('body').addClass('scrollDown');
$(this).data('jScroll.direction', -1);
}
else {
// Trigger the up scroll event
$(this).trigger('jScroll.up');
if(!$(this).data('jScroll.direction')) {
$(this).trigger('jScroll.direction.change');
}
$('body').removeClass('scrollDown');
$('body').addClass('scrollup');
$(this).data('jScroll.direction', 1);
}
// reset lastScrollTop
lastScrollTop = current;
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment