Last active
August 16, 2023 21:04
-
-
Save tusamni/6227c01f0e876b9eee810a48aaea7aaa to your computer and use it in GitHub Desktop.
Tailwind CSS intersection observer for sticky navigation bar
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 banner = document.querySelector("#banner"); | |
const nav = document.querySelector("#navigation"); | |
// Create a new IntersectionObserver object | |
let observer = new IntersectionObserver(function (entries, observer) { | |
for (let entry of entries) { | |
if (entry.isIntersecting) { | |
nav.classList.remove("bg-stone-200"); | |
nav.classList.add("bg-stone-100"); | |
} else { | |
nav.classList.add("bg-stone-200"); | |
nav.classList.remove("bg-stone-100"); | |
} | |
} | |
}); | |
// Observe each heading | |
observer.observe(banner); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment