js/docs: scroll TOC to active menu item

This helps keeping the currently selected menu item into
view on pages that have many menu-items (such as the docker
engine CLI reference)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-10-08 15:03:37 +02:00
parent 86f2259e06
commit c515c773b8
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 25 additions and 0 deletions

View File

@ -108,6 +108,31 @@ function renderNav(docstoc) {
element.innerHTML = outputHorzTabs.join(""); element.innerHTML = outputHorzTabs.join("");
}); });
document.getElementById("jsTOCLeftNav").innerHTML = outputLetNav.join(""); document.getElementById("jsTOCLeftNav").innerHTML = outputLetNav.join("");
// Scroll the current menu item into view. We actually pick the item *above*
// the current item to give some headroom above
scrollMenuItem("#jsTOCLeftNav a.currentPage")
}
// Scroll the given menu item into view. We actually pick the item *above*
// the current item to give some headroom above
function scrollMenuItem(selector) {
let item = document.querySelector(selector)
if (item) {
item = item.closest("li")
}
if (item) {
item = item.previousElementSibling
}
if (item) {
item.scrollIntoView(true)
if (window.location.hash.length < 2) {
// Scrolling the side-navigation may scroll the whole page as well
// this is a dirty hack to scroll the main content back to the top
// if we're not on a specific anchor
document.querySelector("main.col-content").scrollIntoView(true)
}
}
} }
function highlightRightNav(heading) { function highlightRightNav(heading) {