vdoc: fix toc scrolling

pull/5284/head
Daniel Däschle 2020-06-08 13:12:07 +02:00 committed by GitHub
parent 344e9b440a
commit 808975fc86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 6 deletions

View File

@ -6,7 +6,7 @@
active.scrollIntoView({ block: 'center', inline: 'nearest' });
}
}
// setupScrollSpy();
setupScrollSpy();
setupMobileToggle();
setupDarkMode();
setupSearch();
@ -19,7 +19,8 @@ function setupScrollSpy() {
sections.forEach(function(section) {
sectionPositions.push(section.offsetTop);
});
window.addEventListener('scroll', function() {
var scrollPos = 0;
window.addEventListener('scroll', function(e) {
// Reset classes
document.querySelectorAll('.doc-toc a[class="active"]').forEach(function(link) {
link.classList.remove('active');
@ -34,15 +35,18 @@ function setupScrollSpy() {
if (link) {
link.classList.add('active');
var docToc = document.querySelector('.doc-toc');
if (link.scrollIntoViewIfNeeded) {
link.scrollIntoViewIfNeeded();
} else if (link.scrollIntoView) {
link.scrollIntoView();
var tocHeight = docToc.clientHeight;
var scrollTop = docToc.scrollTop;
if ((document.body.getBoundingClientRect()).top < scrollPos && scrollTop < link.offsetTop - 10) {
docToc.scrollTop = link.clientHeight + link.offsetTop - tocHeight + 10;
} else if (scrollTop > link.offsetTop - 10) {
docToc.scrollTop = link.offsetTop - 10;
}
}
break;
}
}
scrollPos = (document.body.getBoundingClientRect()).top;
});
}