Skip to content

Instantly share code, notes, and snippets.

@arielsalminen
Last active April 27, 2016 12:26
Show Gist options
  • Save arielsalminen/8473006 to your computer and use it in GitHub Desktop.
Save arielsalminen/8473006 to your computer and use it in GitHub Desktop.
Simple sticky nav in plain JS
// Simple sticky nav in plain JS
// p.s. this is just a short example, you should remember to debounce the scroll event ;-)
if ("addEventListener" in window && "classList" in document.documentElement) {
var element = document.getElementById("nav"),
offset = element.offsetTop;
window.addEventListener("scroll", function() {
if (offset < window.pageYOffset) {
element.classList.add("fixed");
} else {
element.classList.remove("fixed");
}
}, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment