forked from Archives/Athou_commafeed
throttle scroll listener
This commit is contained in:
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user