From 7498d139ca1e7c5f433da26849015e81fb62eb8c Mon Sep 17 00:00:00 2001 From: Dmytro Kozlov Date: Fri, 23 Jun 2023 12:05:32 +0200 Subject: [PATCH] docs: add scroll to the selected element (#4508) * docs: add scroll to the selected element * docs: scroll to root if element not found * docs: simplify code * docs: code cleanup * docs: fix comments (fix code formatting, check element only inside sidebar container) --- docs/_includes/extra/script.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/docs/_includes/extra/script.js b/docs/_includes/extra/script.js index 0eb90cd9c7..748dd60a70 100644 --- a/docs/_includes/extra/script.js +++ b/docs/_includes/extra/script.js @@ -2,6 +2,26 @@ * doesn't work due to fixed body height when menu is toggled. * This script intercepts clicks on links, toggles the menu off * and performs the anchor navigation. */ + +window.addEventListener("load", function () { + let href = window.location.pathname; + const hash = window.location.hash; + if (hash !== "") { + href = hash + } + const sidebar = document.querySelector('.sidebar .toctree'); + const selector = function(href) {return `a[href="${href}"]`}; + let element = sidebar.querySelector(selector(href)); + if (!element) { + href = window.location.pathname; + element = document.querySelector(selector(href)); + } + if (element) { + element.scrollIntoView({ behavior: "smooth" }); + } +}); + + $(document).on("click", '.shift li.toc a', function(e) { let segments = this.href.split('#'); if (segments.length < 2) { @@ -32,4 +52,4 @@ codes.forEach((code) => { countID++; }); -let clipboard = new ClipboardJS('.btn-copy'); \ No newline at end of file +let clipboard = new ClipboardJS('.btn-copy');