-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Highlight current heading in sidebar TOC
- Loading branch information
Showing
4 changed files
with
70 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function handleScroll() { | ||
const as = [...document.querySelectorAll(".toc a")].reverse(); | ||
const toc = document.querySelector(".toc"); | ||
const scrollable = toc.scrollHeight > toc.clientHeight; | ||
let done = false; | ||
for (const a of as) { | ||
a.style.color = ""; | ||
if (!done) { | ||
const header = document.getElementById(a.href.split("#")[1]); | ||
if (!header) continue; | ||
const top = header.getBoundingClientRect().top; | ||
if (top < window.innerHeight / 5) { | ||
a.style.color = "white"; | ||
if (scrollable) { | ||
a.scrollIntoView({ block: "center" }); | ||
} | ||
done = true; | ||
} | ||
} | ||
} | ||
document.querySelector(".toc-title>a").style.color = done ? "" : "white"; | ||
} | ||
|
||
window.addEventListener("scroll", handleScroll); | ||
handleScroll(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters