# 获取目标容器内的元素列表
const getScrollDom = (params) => {
const { scroller, targets, selector = document.querySelector } = params;
const documentSelector = selector.bind(document);
const scrollerDom = documentSelector(scroller);
const scrollerDomRect = scrollerDom.getBoundingClientRect();
const viewTargets = [];
for (let item of targets) {
const itemDom = documentSelector(item);
if (!itemDom) continue;
const itemDomRect = itemDom.getBoundingClientRect();
if (
itemDomRect.top > scrollerDomRect.top &&
itemDomRect.top < scrollerDomRect.bottom
) {
viewTargets.push(item);
}
if (itemDomRect.top > scrollerDomRect.bottom) {
break;
}
}
return viewTargets;
};
export default getScrollDom;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
使用
const viewTargets = Tools.getScrollDom({
scroller: "content",
targets:['item-1','item-2','item-3']
selector: document.getElementById,
});
1
2
3
4
5
2
3
4
5