Skip to content

Commit

Permalink
fix(toc): optimize header hide handle for anchor jump
Browse files Browse the repository at this point in the history
  • Loading branch information
XPoet committed Nov 7, 2023
1 parent 5d09035 commit 671e4c5
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions source/js/post/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function initTOC() {

// register TOC Nav
registerTocNav() {
const isHideHeader = KEEP.theme_config?.scroll?.hide_header
const register = (tocContainer) => {
return [...tocContainer.querySelectorAll('.post-toc li a.nav-link')].map((element) => {
const target = document.getElementById(
Expand All @@ -46,17 +47,24 @@ function initTOC() {
event.preventDefault()
let winScrollY = window.scrollY
winScrollY = winScrollY <= 1 ? -19 : winScrollY
const offset = target.getBoundingClientRect().top + winScrollY
let offset = target.getBoundingClientRect().top + winScrollY

if (!isHideHeader) {
offset = offset - 60
}

window.anime({
targets: document.scrollingElement,
duration: 500,
easing: 'linear',
scrollTop: offset,
complete: () => {
history.pushState(null, document.title, element.href)
setTimeout(() => {
KEEP.utils.pageTopDom.classList.add('hide')
}, 150)
if (isHideHeader) {
setTimeout(() => {
KEEP.utils.pageTopDom.classList.add('hide')
}, 150)
}
}
})
})
Expand Down

0 comments on commit 671e4c5

Please sign in to comment.