Skip to content

Commit d632b9a

Browse files
add new auto scroll function (#162)
1 parent 97799db commit d632b9a

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/js/01-nav.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@
2929
// Auto scroll the side nav to the current page's side nav link
3030
const currentPageLink = sideNav.querySelector('.nav-link.current-page')
3131
if (currentPageLink) {
32-
currentPageLink.scrollIntoView({ block: 'center' })
32+
const sideNavRect = sideNav.getBoundingClientRect()
33+
const currentPageLinkRect = currentPageLink.getBoundingClientRect()
34+
const offsetTop = currentPageLinkRect.top - sideNavRect.top + sideNav.scrollTop
35+
if (
36+
currentPageLinkRect.top < sideNavRect.top ||
37+
currentPageLinkRect.bottom > (sideNavRect.bottom - 160)
38+
) {
39+
sideNav.scrollTop = offsetTop - (sideNav.clientHeight - 160) / 2 + currentPageLink.clientHeight / 2
40+
}
3341
}
3442
}
3543

0 commit comments

Comments
 (0)