mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 03:32:51 +02:00
34 lines
No EOL
764 B
Text
34 lines
No EOL
764 B
Text
<script>
|
|
let scrollHandler: EventListener | undefined
|
|
|
|
function debounce(fn: () => void, delay: number) {
|
|
let timer: ReturnType<typeof setTimeout> | undefined
|
|
return function () {
|
|
clearTimeout(timer)
|
|
timer = setTimeout(fn, delay)
|
|
}
|
|
}
|
|
|
|
function initScrollbar() {
|
|
const body = document.body
|
|
body.classList.remove('scrolling')
|
|
|
|
if (scrollHandler) {
|
|
window.removeEventListener('scroll', scrollHandler)
|
|
}
|
|
|
|
const hideScrollbar = debounce(() => {
|
|
body.classList.remove('scrolling')
|
|
}, 1500)
|
|
|
|
scrollHandler = () => {
|
|
body.classList.add('scrolling')
|
|
hideScrollbar()
|
|
}
|
|
|
|
window.addEventListener('scroll', scrollHandler, { passive: true })
|
|
}
|
|
|
|
initScrollbar()
|
|
document.addEventListener('astro:page-load', initScrollbar)
|
|
</script> |