Skip to content

Instantly share code, notes, and snippets.

@Lego2012
Forked from leabs/navbar.astro
Created November 30, 2023 17:42
Show Gist options
  • Save Lego2012/1d64ce726a58859344310366c22f479b to your computer and use it in GitHub Desktop.
Save Lego2012/1d64ce726a58859344310366c22f479b to your computer and use it in GitHub Desktop.
Astro - Set active class on Navbar component.
<!--Set active class on nav when the page is active -->
<script>
const nav = document.querySelector(".navbar");
const navLinks = nav.querySelectorAll("a");
const currentURL = window.location.href;
navLinks.forEach((link) => {
if (link.href === currentURL) {
link.classList.add("active");
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment