【发布时间】:2022-01-19 13:42:26
【问题描述】:
我正在为我的 react 应用程序使用无限滚动,并且有这个功能可以检测我何时正好位于页面底部:
const [isFetching, setIsFetching] = useState(false);
// Fire Upon Reaching the Bottom of the Page
const handleScroll = () => {
if (
window.innerHeight +
Math.max(
window.pageYOffset,
document.documentElement.scrollTop,
document.body.scrollTop
) !==
document.documentElement.offsetHeight
)
return;
setIsFetching(true);
};
// Debounce the Scroll Event Function and Cancel it When Called
const debounceHandleScroll = debounce(handleScroll, 100);
useEffect(() => {
window.addEventListener("scroll", debounceHandleScroll);
return () => window.removeEventListener("scroll", debounceHandleScroll);
}, [debounceHandleScroll]);
debounceHandleScroll.cancel();
问题在于当我在手机中加载我的页面时,似乎由于移动浏览器的标签或栏,它没有检测到页面底部,因此内容没有加载。
有什么方法可以检测到用户靠近底部并在那时才触发该功能?
【问题讨论】:
标签: javascript html reactjs