Skip to content

Instantly share code, notes, and snippets.

@noudadrichem
Created September 24, 2018 20:44
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 noudadrichem/fd44def61b0015b6772ac4e9cb91d7a1 to your computer and use it in GitHub Desktop.
Save noudadrichem/fd44def61b0015b6772ac4e9cb91d7a1 to your computer and use it in GitHub Desktop.
const nav = document.querySelector('#nav');
const topOfNav = nav.offsetTop
function fixNav() {
if(window.scrollY >= topOfNav) {
document.body.classList.add('fixed-nav')
document.body.style.paddingTop = nav.offsetHeight + 'px';
} else {
document.body.classList.remove('fixed-nav')
document.body.style.paddingTop = 0;
}
};
window.addEventListener('scroll', fixNav);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment