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 } /**