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