Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 6 additions & 55 deletions src/utils/scrollTo.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,12 @@
// help functions
const easeInOutQuad = (t: number, b: number, c: number, d: number): number => {
t /= d / 2
if (t < 1) return c / 2 * t * t + b
t--
return -c / 2 * (t * (t - 2) - 1) + b
}

const animatedScrollTo = (
element: HTMLElement,
to: number,
duration: number,
callback?: () => void
): void => {
let start = element.scrollTop
let change = to - start
let animationStart = +new Date()
let animating = true
let lastpos: number | null = null

const animateScroll = (): void => {
if (!animating) {
return
}
requestAnimationFrame(animateScroll)
const now = +new Date()
const val = Math.floor(easeInOutQuad(now - animationStart, start, change, duration))
if (lastpos) {
if (lastpos === Math.ceil(element.scrollTop)) {
lastpos = val
element.scrollTop = val
} else {
animating = false
}
} else {
lastpos = val
element.scrollTop = val
}
if (now > animationStart + duration) {
element.scrollTop = to
animating = false
if (callback) {
callback()
}
}
}
requestAnimationFrame(animateScroll)
}

const scrollToElement = (selector: string): void => {
// Scroll to search highlight word
const container = document.documentElement
const anchor = document.querySelector(selector)
if (anchor) {
const { y } = anchor.getBoundingClientRect()
const DURATION = 300
animatedScrollTo(container, container.scrollTop + y - 60, DURATION)
if (!anchor) {
return
}

const headerOffset = 60
const top = anchor.getBoundingClientRect().top + window.pageYOffset - headerOffset
window.scrollTo({ top, behavior: 'smooth' })
}

export default scrollToElement