Last active
January 8, 2020 09:45
-
-
Save bunnyhawk/8bfb39f1fd7206665dcd0e1954f793f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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