Skip to content

Commit

Permalink
add new auto scroll function (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
colegoldsmith authored Sep 5, 2024
1 parent 97799db commit d632b9a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/js/01-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down

0 comments on commit d632b9a

Please sign in to comment.