diff --git a/src/components/common/BasicScripts.astro b/src/components/common/BasicScripts.astro index bc18270..0cc1e4e 100644 --- a/src/components/common/BasicScripts.astro +++ b/src/components/common/BasicScripts.astro @@ -175,11 +175,20 @@ import { UI } from 'astrowind:config'; onPageShow(); }); + // Store smooth scrolling handlers to allow cleanup + let smoothScrollHandlers = new WeakMap(); + // Handle smooth scrolling for anchor links across all pages function setupSmoothScrolling() { // Handle links that start with # (pure anchor links) document.querySelectorAll('a[href^="#"]:not([href="#"])').forEach((anchor) => { - anchor.addEventListener('click', function (e) { + // Remove old handler if exists + const oldHandler = smoothScrollHandlers.get(anchor); + if (oldHandler) { + anchor.removeEventListener('click', oldHandler); + } + + const handler = function (e) { e.preventDefault(); const targetId = this.getAttribute('href').substring(1); @@ -191,12 +200,21 @@ import { UI } from 'astrowind:config'; behavior: 'smooth', }); } - }); + }; + + anchor.addEventListener('click', handler); + smoothScrollHandlers.set(anchor, handler); }); // Handle links that contain # but don't start with it (page + anchor) document.querySelectorAll('a[href*="#"]:not([href^="#"])').forEach((anchor) => { - anchor.addEventListener('click', function (e) { + // Remove old handler if exists + const oldHandler = smoothScrollHandlers.get(anchor); + if (oldHandler) { + anchor.removeEventListener('click', oldHandler); + } + + const handler = function (e) { const href = this.getAttribute('href'); const isHashLink = this.getAttribute('data-hash-link') === 'true'; @@ -240,7 +258,10 @@ import { UI } from 'astrowind:config'; window.location.href = href; } } - }); + }; + + anchor.addEventListener('click', handler); + smoothScrollHandlers.set(anchor, handler); }); } diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro index d6bf49e..99d593f 100644 --- a/src/layouts/Layout.astro +++ b/src/layouts/Layout.astro @@ -88,40 +88,92 @@ const { language, textDirection } = I18N; - -