Skip to content

Instantly share code, notes, and snippets.

@peterkarn
Last active June 24, 2021 08:52
Show Gist options
  • Save peterkarn/d1cb38b7fa4de51bc196b084bdaabe1f to your computer and use it in GitHub Desktop.
Save peterkarn/d1cb38b7fa4de51bc196b084bdaabe1f to your computer and use it in GitHub Desktop.
const
header = document.querySelector('.header');
let lastScrollTop = 0;
window.addEventListener('scroll', () => {
let scrollDistance = window.scrollY;
if (scrollDistance > lastScrollTop) {
header.classList.remove('scrollable');
header.style.opacity = "";
header.style.transform = 'translateY(-10px)';
} else {
// header.style.opacity = 1;
header.style.transform = 'translateY(0)';
header.classList.add('scrollable');
}
lastScrollTop = scrollDistance;
if (lastScrollTop === 0) {
header.classList.remove('scrollable');
header.style.opacity = "";
header.style.transform = 'translateY(0)';
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment