From 00a858761c1e9ed710ef1f59be46928fe4c06b63 Mon Sep 17 00:00:00 2001 From: pengzhanbo Date: Sun, 2 Mar 2025 00:07:18 +0800 Subject: [PATCH] fix(theme): incorrect calculation of active link in the outline, close #492 (#501) --- theme/src/client/composables/outline.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/theme/src/client/composables/outline.ts b/theme/src/client/composables/outline.ts index ffa75d8f..ebb4319a 100644 --- a/theme/src/client/composables/outline.ts +++ b/theme/src/client/composables/outline.ts @@ -187,7 +187,7 @@ export function useActiveAnchor(container: Ref, marker: Ref< if (!isAsideEnabled.value) return - const scrollY = window.scrollY + const scrollY = Math.round(window.scrollY) const innerHeight = window.innerHeight const offsetHeight = document.body.offsetHeight const isBottom = Math.abs(scrollY + innerHeight - offsetHeight) < 1 @@ -222,7 +222,7 @@ export function useActiveAnchor(container: Ref, marker: Ref< // find the last header above the top of viewport let activeLink: string | null = null for (const { link, top } of headers) { - if (top > scrollY + 88) + if (top > scrollY + 92) break activeLink = link @@ -284,18 +284,14 @@ export function useActiveAnchor(container: Ref, marker: Ref< function getAbsoluteTop(element: HTMLElement): number { let offsetTop = 0 - while (element !== document.body) { - if (element === null) { - // child element is: - // - not attached to the DOM (display: none) - // - set to fixed position (not scrollable) - // - body or html element (null offsetParent) - return Number.NaN + while (element && element !== document.body) { + if (window.getComputedStyle(element).position === 'fixed') { + return element.offsetTop } offsetTop += element.offsetTop element = element.offsetParent as HTMLElement } - return offsetTop + return element ? offsetTop : Number.NaN } /**