throttle scroll listener

This commit is contained in:
Athou
2023-06-24 23:04:52 +02:00
parent bdcfbc22bf
commit d5898a0173

View File

@@ -1,3 +1,4 @@
import { throttle } from "throttle-debounce"
import { Category } from "./types" import { Category } from "./types"
export function visitCategoryTree(category: Category, visitor: (category: Category) => void): void { export function visitCategoryTree(category: Category, visitor: (category: Category) => void): void {
@@ -31,15 +32,15 @@ export const scrollToWithCallback = ({ options, onScrollEnded }: { options: Scro
const onScroll = () => { const onScroll = () => {
if (window.scrollY.toFixed() === offset) { if (window.scrollY.toFixed() === offset) {
window.removeEventListener("scroll", onScroll) window.removeEventListener("scroll", throttleListener)
onScrollEnded() onScrollEnded()
} }
} }
const throttleListener = throttle(100, onScroll)
window.addEventListener("scroll", onScroll) window.addEventListener("scroll", throttleListener)
// scrollTo does not trigger if there's nothing to do, trigger it manually // scrollTo does not trigger if there's nothing to do, trigger it manually
onScroll() throttleListener()
window.scrollTo(options) window.scrollTo(options)
} }