kollapsminoriteten/wp-content/themes/blocksy/static/js/frontend/dynamic-chunks/scroll-trigger.js

35 lines
629 B
JavaScript
Raw Normal View History

2025-08-27 08:44:30 +02:00
const handledChunks = {}
export const handleScrollTrigger = (
trigger,
chunk,
loadChunkWithPayload,
loadedChunks
) => {
if (handledChunks[chunk.id]) {
return
}
handledChunks[chunk.id] = true
setTimeout(() => {
let prevScroll = scrollY
let cb = (e) => {
if (
Math.abs(scrollY - prevScroll) > 30 ||
// are we at the bottom of the page?
window.innerHeight + Math.round(scrollY) >=
document.body.offsetHeight
) {
document.removeEventListener('scroll', cb)
loadChunkWithPayload(chunk)
return
}
}
cb()
document.addEventListener('scroll', cb, { passive: true })
}, 500)
}