From f8c7bd29ad0198f907df114184244916740fdf80 Mon Sep 17 00:00:00 2001 From: Jazee6 Date: Sat, 28 Dec 2024 22:48:18 +0800 Subject: [PATCH] fix: toc --- src/pages/posts/[...slug].astro | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pages/posts/[...slug].astro b/src/pages/posts/[...slug].astro index fa2127c..b9c7857 100644 --- a/src/pages/posts/[...slug].astro +++ b/src/pages/posts/[...slug].astro @@ -74,24 +74,24 @@ const haveToc = headings.some(heading => heading.depth === 2 || heading.depth == const links = document.querySelectorAll('.menu-item'); const handler = debounce(() => { const scrollY = window.scrollY; - if (scrollY < 84) { + if (scrollY < 80) { links.forEach(link => link.classList.remove('bg-hover')); links[0].classList.add('bg-hover'); history.replaceState(null, '', window.location.pathname); return; } - links.forEach(link => { + for (const link of links) { const href = link.getAttribute('href') as string; - const target = document.querySelector(href); + const target = document.querySelector(`[id="${href.slice(1)}"]`); if (target) { - const offsetTop = target.getBoundingClientRect().top + scrollY - 84 + const offsetTop = target.getBoundingClientRect().top + scrollY - 80 if (scrollY >= offsetTop) { links.forEach(link => link.classList.remove('bg-hover')); link.classList.add('bg-hover'); history.replaceState(null, '', href); } } - }); + } }) document.addEventListener('scroll', handler) document.addEventListener('DOMContentLoaded', handler)