Skip to content

Instantly share code, notes, and snippets.

@sinrise
Last active April 11, 2017 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinrise/9ba66212b67c9970e19a884b390f834a to your computer and use it in GitHub Desktop.
Save sinrise/9ba66212b67c9970e19a884b390f834a to your computer and use it in GitHub Desktop.
Alternative to a persistent scroll event listener. This fires only when the user scrolls, then stops after 250ms.
var timeout = null;
$(window).scroll(function () {
if(!timeout) {
timeout = setTimeout(function() {
clearTimeout(timeout);
timeout = null;
if ($(window).scrollTop() >= $("#navTtigger").offset().top) {
$("nav").addClass("fixed");
} else { $("nav").removeClass("fixed"); }
}, 250);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment