mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-17 12:01:33 +02:00
feat: add auto-hide native scrollbar and optimize scrollbar styling
This commit is contained in:
parent
41fca8569e
commit
168e231119
7 changed files with 82 additions and 12 deletions
34
src/components/Widgets/Scrollbar.astro
Normal file
34
src/components/Widgets/Scrollbar.astro
Normal file
|
@ -0,0 +1,34 @@
|
|||
<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')
|
||||
}, 1200)
|
||||
|
||||
scrollHandler = () => {
|
||||
body.classList.add('scrolling')
|
||||
hideScrollbar()
|
||||
}
|
||||
|
||||
window.addEventListener('scroll', scrollHandler, { passive: true })
|
||||
}
|
||||
|
||||
initScrollbar()
|
||||
document.addEventListener('astro:page-load', initScrollbar)
|
||||
</script>
|
Loading…
Add table
Add a link
Reference in a new issue