Skip to content

Instantly share code, notes, and snippets.

@gerardreches
Last active September 5, 2023 20:48
Show Gist options
  • Save gerardreches/e02a2d616d8d91da1be070c87798c8b4 to your computer and use it in GitHub Desktop.
Save gerardreches/e02a2d616d8d91da1be070c87798c8b4 to your computer and use it in GitHub Desktop.
This code helps to apply custom CSS for sticky headers by adding a class named "page-scrolled" to the body tag when the user is scrolling through the page.
// This code helps to apply custom CSS for sticky headers by adding a class named "page-scrolled" to the body tag when the user is scrolling through the page.
<script>
var lastScrollYPosition = 0;
window.addEventListener('scroll', function() {
if (window.scrollY > 0) {
document.body.classList.add('page-scrolled');
} else {
document.body.classList.remove('page-scrolled');
}
if (window.scrollY > lastScrollYPosition) {
document.body.classList.add('page-scrolled-forward');
document.body.classList.remove('page-scrolled-backward');
} else {
document.body.classList.remove('page-scrolled-forward');
document.body.classList.add('page-scrolled-backward');
}
lastScrollYPosition = window.scrollY;
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment