Skip to content

Commit

Permalink
Scroll to the active item when the navbar is too long (#2340)
Browse files Browse the repository at this point in the history
Co-authored-by: chalin <[email protected]>
Co-authored-by: Phillip Carter <[email protected]>
  • Loading branch information
3 people authored Mar 24, 2023
1 parent 2a00d4f commit cef971b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
17 changes: 17 additions & 0 deletions assets/js/navScroll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Check If an Element is Visible in the Viewport
function isInViewport(element) {
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <=
(window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}

let ele = document.querySelector('#td-section-nav .td-sidebar-nav-active-item');

if (ele && !isInViewport(ele)) {
ele.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'nearest' });
}
1 change: 1 addition & 0 deletions layouts/partials/hooks/body-end.html
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
{{ partial "script.html" (dict "src" "js/tracing.js") -}}
{{ partial "script.html" (dict "src" "js/navScroll.js") -}}

0 comments on commit cef971b

Please sign in to comment.