Skip to content

Instantly share code, notes, and snippets.

@tusamni
Last active August 16, 2023 21:04
Show Gist options
  • Save tusamni/6227c01f0e876b9eee810a48aaea7aaa to your computer and use it in GitHub Desktop.
Save tusamni/6227c01f0e876b9eee810a48aaea7aaa to your computer and use it in GitHub Desktop.
Tailwind CSS intersection observer for sticky navigation bar
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