diff --git a/assets/js/navScroll.js b/assets/js/navScroll.js new file mode 100644 index 000000000000..4fc710164b20 --- /dev/null +++ b/assets/js/navScroll.js @@ -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' }); +} diff --git a/layouts/partials/hooks/body-end.html b/layouts/partials/hooks/body-end.html index 4496dbb9cec0..fe43acb45b60 100644 --- a/layouts/partials/hooks/body-end.html +++ b/layouts/partials/hooks/body-end.html @@ -1 +1,2 @@ {{ partial "script.html" (dict "src" "js/tracing.js") -}} +{{ partial "script.html" (dict "src" "js/navScroll.js") -}}