From 428fde960708705fa29e0ff20cac81a5a4103867 Mon Sep 17 00:00:00 2001 From: Cole Goldsmith Date: Thu, 5 Sep 2024 16:18:57 -0500 Subject: [PATCH] add new auto scroll function --- src/js/01-nav.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/js/01-nav.js b/src/js/01-nav.js index 1f5aea50..5fc4acb5 100644 --- a/src/js/01-nav.js +++ b/src/js/01-nav.js @@ -29,7 +29,15 @@ // Auto scroll the side nav to the current page's side nav link const currentPageLink = sideNav.querySelector('.nav-link.current-page') if (currentPageLink) { - currentPageLink.scrollIntoView({ block: 'center' }) + const sideNavRect = sideNav.getBoundingClientRect() + const currentPageLinkRect = currentPageLink.getBoundingClientRect() + const offsetTop = currentPageLinkRect.top - sideNavRect.top + sideNav.scrollTop + if ( + currentPageLinkRect.top < sideNavRect.top || + currentPageLinkRect.bottom > (sideNavRect.bottom - 160) + ) { + sideNav.scrollTop = offsetTop - (sideNav.clientHeight - 160) / 2 + currentPageLink.clientHeight / 2 + } } }