I have an anchor tag on a page in my Svelte app. The link to the anchor works on the page itself, but I can't link to the anchor from another page. And when I enter the URL with the anchor, the page doesn't scroll down. I need to be able to give people a link and have them go to a specific part of the page.
Here's the relevant code:
<script>
function scrollIntoView({ target }) {
const el = document.querySelector(target.getAttribute("href"));
if (!el) return;
el.scrollIntoView({
behavior: "smooth",
});
}
</script>
<nav>
<a href="#here" on:click|preventDefault={scrollIntoView}>go to anchor</a>
</nav>
<main>
<section id="section-1">
... lots of lorem ipsum ...
</section>
<section>
<h2 id="here">anchor</h2>
And I have a REPL here: https://svelte.dev/repl/e651218bdb47455d9cafe8bff27c8d7b?version=3.24.0
I'm using page.js for my routing to components -- I haven't found anything specific about targeting anchor tags in the documentation.
Any help would be greatly appreciated.