mirror of
https://0xacab.org/anarsec/anarsec.guide.git
synced 2025-06-23 14:04:08 -04:00
improve toc js
This commit is contained in:
parent
89dd57f725
commit
27cbe3f4fd
3 changed files with 20 additions and 3 deletions
|
@ -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');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue