-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.js
56 lines (52 loc) · 1.59 KB
/
transform.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const { useState, useRef, useMemo, useEffect } = React;
const ITEM_HEIGHT = 60;
const LIST_LENGTH = 100000;
function App() {
const [list] = useState(
new Array(LIST_LENGTH).fill(0).map((_, idx) => ({ idx: idx + 1 }))
);
return (
<div>
<VirtualList list={list} itemHeight={ITEM_HEIGHT}></VirtualList>
</div>
);
}
// react-list https://github.com/caseywebdev/react-list
function VirtualList(props) {
const { list, itemHeight } = props;
const [start, setStart] = useState(0);
const [count, setCount] = useState(0);
const scrollRef = useRef(null);
const contentRef = useRef(null);
const totalHeight = useMemo(() => itemHeight * list.length, [list.length]);
useEffect(() => {
setCount(Math.ceil(scrollRef.current.clientHeight / itemHeight));
}, []);
const scrollHandle = () => {
const { scrollTop } = scrollRef.current;
const newStart = Math.floor(scrollTop / itemHeight);
setStart(newStart);
contentRef.current.style.transform = `translate3d(0, ${
newStart * itemHeight
}px, 0)`;
};
const subList = list.slice(start, start + count);
return (
<div className="virtual-list" onScroll={scrollHandle} ref={scrollRef}>
<div style={{ height: totalHeight + "px" }}>
<div className="content" ref={contentRef}>
{subList.map(({ idx }) => (
<div
key={idx}
className="item"
style={{ height: itemHeight + "px" }}
>
{idx}
</div>
))}
</div>
</div>
</div>
);
}
ReactDOM.render(<App />, document.querySelector("#app"));