Skip to content

Instantly share code, notes, and snippets.

@lislis
Created January 16, 2019 17:45
Show Gist options
  • Save lislis/01fabc6a027c052b8876781782491116 to your computer and use it in GitHub Desktop.
Save lislis/01fabc6a027c052b8876781782491116 to your computer and use it in GitHub Desktop.
Fixed nav fade out on scroll-down, fade in on scroll up, vanilla js
// based on the jquery version of https://medium.com/@mariusc23/hide-header-on-scroll-down-show-on-scroll-up-67bbaae9a78c
var didScroll;
var lastScrollTop = 0;
var delta = 8; // to prevent 'accidental' trigger
var navbarHeight = 85; // probably in css
var navbarElem = document.querySelector('.navbar');
window.addEventListener('scroll', function (ev) {
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 300); // debounce
function hasScrolled() {
var st = window.scrollY;
if (Math.abs(lastScrollTop  -  st) <= delta)
return;
if (st > lastScrollTop && st > navbarHeight) {
navbarElem.classList.add('is-up');
} else {
if(st - window.innerHeight < window.outerHeight) {
navbarElem.classList.remove('is-up');
}
}
lastScrollTop = st;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment