fix: 改进瀑布流滚动和回到顶部的可靠性

- 使用 ref 避免 loadMore 依赖导致事件重复注册
- 同时监听 window 和 document 的 scroll 事件
- 降低回到顶部按钮触发阈值到 400px
- 增大触底检测距离到 300px

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
kkfluous
2026-04-01 23:52:38 +08:00
parent 82dac759be
commit b7b254546c

View File

@@ -179,19 +179,26 @@ export default function MonitoringView() {
}, [loadFirstPage]); }, [loadFirstPage]);
// 滚动触底检测 + 回到顶部按钮 // 滚动触底检测 + 回到顶部按钮
const loadMoreRef = useRef(loadMore);
loadMoreRef.current = loadMore;
useEffect(() => { useEffect(() => {
const handleScroll = () => { const handleScroll = () => {
const scrollY = window.scrollY; const scrollY = window.scrollY || document.documentElement.scrollTop;
setShowBackToTop(scrollY > 600); setShowBackToTop(scrollY > 400);
const scrollHeight = document.documentElement.scrollHeight; const scrollHeight = document.documentElement.scrollHeight;
const clientHeight = window.innerHeight; const clientHeight = window.innerHeight;
if (scrollHeight - scrollY - clientHeight < 200) { if (scrollHeight - scrollY - clientHeight < 300) {
loadMore(); loadMoreRef.current();
} }
}; };
window.addEventListener('scroll', handleScroll, { passive: true }); window.addEventListener('scroll', handleScroll, { passive: true });
return () => window.removeEventListener('scroll', handleScroll); document.addEventListener('scroll', handleScroll, { passive: true });
}, [loadMore]); return () => {
window.removeEventListener('scroll', handleScroll);
document.removeEventListener('scroll', handleScroll);
};
}, []);
const scrollToTop = () => { const scrollToTop = () => {
window.scrollTo({ top: 0, behavior: 'smooth' }); window.scrollTo({ top: 0, behavior: 'smooth' });