Created
April 17, 2021 11:09
-
-
Save zaydek/15a5bfb108537f26a267ecdabb8a2fc3 to your computer and use it in GitHub Desktop.
Probably a bad idea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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