Created
September 25, 2013 14:13
-
-
Save Plou/6700228 to your computer and use it in GitHub Desktop.
Detect and fire scrollDown & scrollUp events
This file contains hidden or 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
| (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