glitchier but nicer toc

This commit is contained in:
Ash Keel 2023-03-01 14:20:15 +01:00
parent 3ec4a8df68
commit a0fc21db39
No known key found for this signature in database
GPG Key ID: BAD8D93E7314ED3E
1 changed files with 9 additions and 7 deletions

View File

@ -14,16 +14,18 @@ if (toc) {
toc.parentElement.remove();
} else {
const observer = new IntersectionObserver((entries) => {
const filtered = entries.filter((i) => getId(i.target));
filtered.forEach((entry) => {
const id = getId(entry.target);
if (id && entry.intersectionRatio > 0) {
toc.querySelector(`li.active`)?.classList.remove("active");
const filtered = entries.filter(
(i) => getId(i.target) && i.intersectionRatio > 0
);
if (filtered.length > 0) {
toc.querySelector(`li.active`)?.classList.remove("active");
filtered.forEach((entry) => {
const id = getId(entry.target);
toc
.querySelector(`li a[href="#${id}"]`)
.parentElement.classList.add("active");
}
});
});
}
});
// Track all sections that have an `id` applied