I am making an infinite scroll component in react. I check (offsetHeight + scrollTop == scrollHeight) to realize scrolled to the end. However, when scrolling to the end sometimes offsetHeight + scrollTop is not equal to scrollHeight.
const handleScroll = async (e) => {
const { offsetHeight, scrollTop, scrollHeight } = e.target;
console.log(offsetHeight + scrollTop + " == " + scrollHeight);
if (offsetHeight + scrollTop == scrollHeight) {
console.log("ok");
setStep((prev) => prev + step);
fetchData(nowStep + step);
}
};
return (
<div
onScroll={handleScroll}
style={{
height: height,
overflowY: "scroll",
overflowX: "hidden",
width: "100%",
padding: 0,
margin: 0,
textAlign: "center",
}}
>
{children}
</div>
Here's what I think:
Edit: I realized the browser zoom could be the cause. At some zoom levels eg: 125%, 100%, 75%, 67% the code runs fine, but at 110%, 90%, 80%, the check results are not as expected. Console when scrolling to the end:
- 110% zoom browser:
690.9090881347656 == 696
694.5454406738281 == 696
695.757568359375 == 696
- 100% zoom browser:
694.6666564941406 == 696
695.3333435058594 == 696
696 == 696
ok
- 90% zoom browser:
694.0740966796875 == 696
694.8148498535156 == 696
695.5555725097656 == 696
