Skip to content

Instantly share code, notes, and snippets.

@ahmadthedev
Created February 3, 2022 22:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadthedev/30a9644f72aaf8dc8f49a023f131d539 to your computer and use it in GitHub Desktop.
Save ahmadthedev/30a9644f72aaf8dc8f49a023f131d539 to your computer and use it in GitHub Desktop.
<header class="hdr_top"></header>
<style>
.hdr_top { position: fixed; top: 0; left: 0; right: 0; z-index: 2; transition: transform 0.4s; background-color: #000; }
.scroll-down .hdr_top { transform: translate3d(0, -100%, 0); }
.scroll-up .hdr_top { padding: 10px 0; }
</style>
<script>
// Sticky Header
const body = document.body;
const scrollUp = "scroll-up";
const scrollDown = "scroll-down";
let lastScroll = 0;
window.addEventListener("scroll", () => {
const currentScroll = window.pageYOffset;
if (currentScroll <= 0) {
body.classList.remove(scrollUp);
return;
}
if (currentScroll > lastScroll && !body.classList.contains(scrollDown)) {
// down
body.classList.remove(scrollUp);
body.classList.add(scrollDown);
lottiePlayer.play();
} else if (
currentScroll < lastScroll &&
body.classList.contains(scrollDown)
) {
// up
body.classList.remove(scrollDown);
body.classList.add(scrollUp);
}
lastScroll = currentScroll;
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment