Skip to content

Instantly share code, notes, and snippets.

@bunnyhawk
Last active January 8, 2020 09:45
Show Gist options
  • Save bunnyhawk/8bfb39f1fd7206665dcd0e1954f793f2 to your computer and use it in GitHub Desktop.
Save bunnyhawk/8bfb39f1fd7206665dcd0e1954f793f2 to your computer and use it in GitHub Desktop.
const header = document.querySelector('.header');
const pageWrap = document.querySelector('.wrapper');
var lastScrollTop = 0;
function debounce(func, wait) {
let timeout;
return function(...args) {
const context = this;
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(context, args), wait);
};
}
function onScroll() {
let currScrollTop = pageWrap.scrollTop;
let isScrollingDown = currScrollTop > lastScrollTop;
let isHeaderVisible = currScrollTop < header.height;
header.classList.toggle('is-hidden', isScrollingDown && !isHeaderVisible);
lastScrollTop = currScrollTop;
}
pageWrap.addEventListener('scroll', debounce(onScroll, 16));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment