Skip to content

Commit

Permalink
fix: TOC scrolls with view
Browse files Browse the repository at this point in the history
Ticket: ENT-11202
Changelog: None
Signed-off-by: Mikita Pilinka <[email protected]>
  • Loading branch information
mineralsfree committed Feb 13, 2024
1 parent 1f131a1 commit 6055d2a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions generator/_assets/js/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,19 +388,25 @@ document.addEventListener("DOMContentLoaded", function () {

for (let i = anchorsOffsets.length - 1; i >= 0; i--) {
if (scrollTop > anchorsOffsets[i]) {
setActiveLink(anchors[i].id);
setActiveLink(anchors[i].id, i);
break;
}
}
}, 50); // 0.05s threshold

}

const setActiveLink = (id) => {
const setActiveLink = (id, n) => {
const activeLink = document.querySelector(`#TOCbox_list li a[href$="#${id}"]`);
if (activeLink) {
activeLink.classList.add('current');
}
const tocWrapper = document.getElementById('TOCbox_wrapper');
const TOC_TOP_OFFSET = 42;
const LI_HEIGHT = 22;
const windowHeight = window.innerHeight;
const bottomLi = windowHeight-TOC_TOP_OFFSET - (LI_HEIGHT * n)
tocWrapper.style.top = (bottomLi > 0 ? 12-tocWrapper.style.top : 12) + 'px';
}

window.addEventListener('scroll', updateActiveTocItem);
Expand Down

0 comments on commit 6055d2a

Please sign in to comment.