improve toc js

This commit is contained in:
anarsec 2023-07-07 02:07:42 +00:00
parent 89dd57f725
commit 27cbe3f4fd
No known key found for this signature in database
3 changed files with 20 additions and 3 deletions

View file

@ -20,11 +20,28 @@
return (c.top <= h) && (n.top - menuBarHeight >= 0);
}
function highlightTocItem(tocIndex) {
const bottomGap = 50;
tocItems[tocIndex].classList.add('is-active');
const bodyRect = document.body.getBoundingClientRect();
const elemRect = tocItems[tocIndex].getBoundingClientRect();
const menuRect = document.querySelector(".menu").getBoundingClientRect();
const menuAbsTop = menuRect.top - bodyRect.top;
const elemAbsBottom = elemRect.bottom - bodyRect.top;
const h = (window.innerHeight || document.documentElement.clientHeight);
if(elemAbsBottom > menuAbsTop + h - 48 - bottomGap){
document.querySelector(".menu").style = "position: sticky; top: " + (menuAbsTop - elemAbsBottom + h - bottomGap).toString() + "px";
}
else{
document.querySelector(".menu").style = "position: sticky; top: 48px;";
}
}
function activateIfVisible() {
let b = true;
for (let i = 0; i < tocItems.length; i++) {
if (b && isVisible(i)) {
tocItems[i].classList.add('is-active');
highlightTocItem(i);
b = false;
} else
tocItems[i].classList.remove('is-active');