Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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