Skip to content

Commit

Permalink
#3209 Make the sidebar scroll into view of the active item
Browse files Browse the repository at this point in the history
Signed-off-by: James Hunt <[email protected]>
  • Loading branch information
thetwopct committed Dec 29, 2024
1 parent ad4651a commit 040e14b
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions layouts/partials/sidebar-tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,17 @@
{{- end }}

<script>
document.addEventListener("DOMContentLoaded", function () {
const activeLink = document.querySelector(".td-sidebar-nav__section .active");
if (activeLink) {
activeLink.scrollIntoView({
behavior: "smooth",
block: "center",
});
}
});
</script>
document.addEventListener("DOMContentLoaded", function () {
const sidebar = document.querySelector("#td-section-nav");
const activeLink = sidebar?.querySelector(".td-sidebar-nav__section .active");

if (activeLink && sidebar) {
const sidebarTop = sidebar.scrollTop;
const activeLinkOffset = activeLink.offsetTop;
sidebar.scrollTo({
top: activeLinkOffset - sidebar.clientHeight / 2 + activeLink.offsetHeight / 2,
behavior: "smooth",
});
}
});
</script>

0 comments on commit 040e14b

Please sign in to comment.