-
-
Save dylanhthomas/43f4536cc04392ff8725388849369670 to your computer and use it in GitHub Desktop.
jQuery - Adds scroll direction to scroll event
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 ($) { | |
"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