Skip to content

Instantly share code, notes, and snippets.

@marcojetson
Created January 16, 2014 15:59
Show Gist options
  • Save marcojetson/8457420 to your computer and use it in GitHub Desktop.
Save marcojetson/8457420 to your computer and use it in GitHub Desktop.
jQuery - Adds scroll direction to scroll event
(function ($) {
"use strict";
var wnd = $(window),
lastScrollTop = wnd.scrollTop(),
lastScrollLeft = wnd.scrollLeft();
wnd.on("scroll", function (event) {
var scrollTop = wnd.scrollTop(),
scrollLeft = wnd.scrollLeft(),
direction = "";
if (scrollTop > lastScrollTop) {
direction += "south";
} else if (scrollTop < lastScrollTop) {
direction += "north";
}
if (scrollLeft > lastScrollLeft) {
direction += "east";
} else if (scrollLeft < lastScrollLeft) {
direction += "west";
}
lastScrollTop = scrollTop;
lastScrollLeft = scrollLeft;
event.direction = direction;
});
}(jQuery));
$(window).scroll(function (e) {
if (-1 !== $.inArray(e.direction, ["south", "southeast", "southwest"])) {
console.log("Going down");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment