Skip to content

Commit 5dc4ef8

Browse files
committed
fix: detect some event when dom change
1 parent 95cd83d commit 5dc4ef8

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/useScrollWatch.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,31 @@ export const useScrollWatch = (props: useScrollWathType) => {
6565

6666
return -1;
6767
};
68-
const refreshScrollTop = debounce(() => {
68+
const refresh = debounce(() => {
6969
setScrollTop(getScrollTop());
70+
setPosList(getPosList());
7071
}, 100);
7172

7273
const [posList, setPosList] = useState<number[]>([]);
7374

7475
useEffect(() => {
75-
setPosList(getPosList());
76+
refresh();
7677
}, [ref]);
7778

7879
const curIndex = getCurIndex(scrollTop, posList);
7980

8081
useEffect(() => {
8182
if (!ref.current) return;
8283
const elm = isWindowScrollParent(ref.current) ? window : ref.current;
83-
const observer = new window.MutationObserver(debounce(getPosList, 1000));
84+
const observer = new window.MutationObserver(refresh);
8485
observer.observe(ref.current, {
8586
childList: true,
8687
subtree: true,
8788
});
88-
elm.addEventListener('scroll', refreshScrollTop);
89+
elm.addEventListener('scroll', refresh);
8990
return () => {
9091
observer.disconnect();
91-
elm && elm.removeEventListener('scroll', refreshScrollTop);
92+
elm && elm.removeEventListener('scroll', refresh);
9293
};
9394
}, [ref]);
9495

0 commit comments

Comments
 (0)