Skip to content

Instantly share code, notes, and snippets.

@zaydek
Created April 17, 2021 11:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaydek/15a5bfb108537f26a267ecdabb8a2fc3 to your computer and use it in GitHub Desktop.
Save zaydek/15a5bfb108537f26a267ecdabb8a2fc3 to your computer and use it in GitHub Desktop.
Probably a bad idea
React.useEffect(() => {
let animationFrame = 0
function virtualScroller(targets, { offset, topOffset, bottomOffset } = {}) {
offset ??= 0
topOffset ??= offset ?? 0
bottomOffset ??= offset ?? 0
cancelAnimationFrame(animationFrame)
animationFrame = window.requestAnimationFrame(() => {
for (let x = 0, len = targets.length; x < len; x++) {
let target = targets[x]
const { top, bottom } = target.getBoundingClientRect()
//
// +----------+ <- offset
// |//////////|
// +----------+ <- t
// | |
// | |
// +----------+ <- b
// |//////////|
// +----------+ <- offset
//
const t = -1 * bottom
const b = (window.scrollY + top) - (window.scrollY + window.innerHeight)
if (t > topOffset) {
target.style.visibility = "hidden"
} else if (b > bottomOffset) {
target.style.visibility = "hidden"
for (x++; x < len; x++) {
target = targets[x]
target.style.visibility = "hidden"
}
return
} else {
target.style.visibility = ""
}
}
})
}
const targets = document.getElementsByClassName("searchResultsBtn")
function handleScroll(e) {
virtualScroller(targets, {
offset: 2 * window.innerHeight,
})
}
virtualScroller(targets, {
offset: 2 * window.innerHeight,
})
document.addEventListener("scroll", handleScroll, false)
return () => document.removeEventListener("scroll", handleScroll, false)
}, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment