fix(theme): incorrect calculation of active link in the outline, close #492 (#501)

This commit is contained in:
pengzhanbo 2025-03-02 00:07:18 +08:00 committed by GitHub
parent 6e1992f811
commit 00a858761c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -187,7 +187,7 @@ export function useActiveAnchor(container: Ref<HTMLElement | null>, 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<HTMLElement | null>, 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<HTMLElement | null>, 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
}
/**