Skip to content

Instantly share code, notes, and snippets.

@gearsdigital
Forked from arielsalminen/sticky-nav.js
Created April 27, 2016 12:26
Show Gist options
  • Save gearsdigital/1195161053463cbf0a9e4392550f9801 to your computer and use it in GitHub Desktop.
Save gearsdigital/1195161053463cbf0a9e4392550f9801 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