SvelteKit $navigating store not working on anchor links

Viewed 19

I have an app in SvelteKit, and I have a navbar that I want to collapse when the user clicks a link the current code that I have is this:

$: if ($navigating) collapseNavbar()

This part only executes (collapses the navbar) when the url changes (from /about to /blog, for example) but not when the anchor changes (ex. from /about#person1 to /about#person2). Is there any way that I can also get it to respond when the anchor link changes?

1 Answers

The window has a hashchange event that you could listen to. Alternatively, maybe you could just collapse on any click outside of the navbar (if that fits the intended logic).

Related