mirror of
https://github.com/nunocoracao/blowfish.git
synced 2026-01-30 15:31:52 +00:00
Merge pull request #2207 from ZhenShuo2021/fix/toc-active
🐛 Fix TOC Active State Loss with Dynamic Content
This commit is contained in:
@@ -33,11 +33,16 @@
|
|||||||
|
|
||||||
function getActiveAnchorId(anchors, offsetRatio) {
|
function getActiveAnchorId(anchors, offsetRatio) {
|
||||||
const threshold = window.scrollY + window.innerHeight * offsetRatio
|
const threshold = window.scrollY + window.innerHeight * offsetRatio
|
||||||
|
const tocLinks = [...document.querySelectorAll('#TableOfContents a[href^="#"]')]
|
||||||
|
const tocIds = new Set(tocLinks.map(link => link.getAttribute('href').substring(1)))
|
||||||
|
|
||||||
for (let i = anchors.length - 1; i >= 0; i--) {
|
for (let i = anchors.length - 1; i >= 0; i--) {
|
||||||
const top = anchors[i].getBoundingClientRect().top + window.scrollY
|
const top = anchors[i].getBoundingClientRect().top + window.scrollY
|
||||||
if (top <= threshold) return anchors[i].id
|
if (top <= threshold && tocIds.has(anchors[i].id)) {
|
||||||
|
return anchors[i].id
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return anchors[0]?.id || ''
|
return anchors.find(anchor => tocIds.has(anchor.id))?.id || ''
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateTOC({ toc, anchors, links, scrollOffset, collapseInactive }) {
|
function updateTOC({ toc, anchors, links, scrollOffset, collapseInactive }) {
|
||||||
|
|||||||
Reference in New Issue
Block a user