|
| 1 | +/** |
| 2 | + * Patch of scrollToActive of sphinxbook theme |
| 3 | + * Scroll to active navigation item |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * A helper function to load scripts when the DOM is loaded. |
| 8 | + * This waits for everything to be on the page first before running, since |
| 9 | + * some functionality doesn't behave properly until everything is ready. |
| 10 | + */ |
| 11 | + var sbRunWhenDOMLoaded = (cb) => { |
| 12 | + if (document.readyState != "loading") { |
| 13 | + cb(); |
| 14 | + } else if (document.addEventListener) { |
| 15 | + document.addEventListener("DOMContentLoaded", cb); |
| 16 | + } else { |
| 17 | + document.attachEvent("onreadystatechange", function () { |
| 18 | + if (document.readyState == "complete") cb(); |
| 19 | + }); |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Sidebar scroll on load. |
| 25 | + * |
| 26 | + * Detect the active page in the sidebar, and scroll so that it is centered on |
| 27 | + * the screen. |
| 28 | + */ |
| 29 | + var scrollToActive = () => { |
| 30 | + var navbar = document.getElementById("site-navigation"); |
| 31 | + var navbar_scrollable = navbar.children[0]; |
| 32 | + var active_pages = navbar.querySelectorAll(".active"); |
| 33 | + var active_page = active_pages[active_pages.length - 1]; |
| 34 | + // Only scroll the navbar if the active link is lower than 50% of the page |
| 35 | + if ( |
| 36 | + active_page !== undefined && |
| 37 | + active_page.offsetTop > $(window).height() * 0.5 |
| 38 | + ) { |
| 39 | + navbar_scrollable.scrollTop = |
| 40 | + active_page.offsetTop - $(window).height() * 0.2; |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | + |
| 45 | +sbRunWhenDOMLoaded(scrollToActive); |
| 46 | + |
| 47 | +$(document).ready(function() { |
| 48 | + console.debug("debug patch_scrollToActive") |
| 49 | +}); |
0 commit comments