Skip to content

Commit 9124e5b

Browse files
committed
refactor: 重构代码
1 parent 82e55ff commit 9124e5b

File tree

1 file changed

+8
-12
lines changed
  • packages/hooks/src/useInfiniteScroll

1 file changed

+8
-12
lines changed

packages/hooks/src/useInfiniteScroll/index.tsx

+8-12
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,14 @@ const useInfiniteScroll = <TData extends Data>(
106106
};
107107

108108
const scrollMethod = () => {
109-
let el = getTargetElement(target);
110-
if (!el) {
111-
return;
112-
}
113-
114-
el = el === document ? document.documentElement : el;
109+
const el = getTargetElement(target);
110+
if (!el) return;
115111

116-
const scrollTop = getScrollTop(el);
117-
const scrollHeight = getScrollHeight(el);
118-
const clientHeight = getClientHeight(el);
112+
const targetEl = el === document ? document.documentElement : el;
113+
const scrollTop = getScrollTop(targetEl);
114+
const scrollHeight = getScrollHeight(targetEl);
115+
const clientHeight = getClientHeight(targetEl);
119116

120-
if (!isScrollToTop && scrollHeight - scrollTop <= clientHeight + threshold) {
121-
loadMore();
122-
}
123117
if (isScrollToTop) {
124118
if (
125119
lastScrollTop.current !== undefined &&
@@ -130,6 +124,8 @@ const useInfiniteScroll = <TData extends Data>(
130124
}
131125
lastScrollTop.current = scrollTop;
132126
scrollBottom.current = scrollHeight - scrollTop;
127+
} else if (scrollHeight - scrollTop <= clientHeight + threshold) {
128+
loadMore();
133129
}
134130
};
135131

0 commit comments

Comments
 (0)