Created
February 3, 2022 22:46
-
-
Save ahmadthedev/30a9644f72aaf8dc8f49a023f131d539 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<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