Наш скролинг

This commit is contained in:
2026-05-17 13:01:30 +03:00
parent a31d35508a
commit 50becd2dca
2 changed files with 12 additions and 5 deletions
+10 -3
View File
@@ -3,12 +3,16 @@ import './App.scss'
function VirtualList({ items, itemHeight=50, containerHeight=400 }) { function VirtualList({ items, itemHeight=50, containerHeight=400 }) {
const containerRef = useRef(null) const containerRef = useRef(null)
//Положение большого нивидимого контейнера
const [scrollTop,setScrollTop] = useState(0); const [scrollTop,setScrollTop] = useState(0);
//Количество отображаемых элементов
const visibleCount = Math.ceil(containerHeight / itemHeight); const visibleCount = Math.ceil(containerHeight / itemHeight);
//Общая высота нивидимого контейнера
const totalHeight = items.length * itemHeight; const totalHeight = items.length * itemHeight;
//Начало и конец подгружаемых(отображаемых) элементов
const startIndex = Math.floor(scrollTop / itemHeight); const startIndex = Math.floor(scrollTop / itemHeight);
const endIndex = Math.min(startIndex + visibleCount + 1, items.length); const endIndex = Math.min(startIndex + visibleCount + 1, items.length);
//Срез видимых элементов из общего списка
const visibleItems = items.slice(startIndex,endIndex); const visibleItems = items.slice(startIndex,endIndex);
const handleScroll = e => { const handleScroll = e => {
@@ -20,8 +24,8 @@ function VirtualList({ items, itemHeight=50, containerHeight=400 }) {
<div ref={containerRef} className="container" style={{ <div ref={containerRef} className="container" style={{
height: containerHeight height: containerHeight
}} onScroll={handleScroll}> }} onScroll={handleScroll}>
<div style={{ height:totalHeight, position: 'relative' }}> <div style={{ height:totalHeight }}>
<div style={{position:'absolute', top: 0, left:0, right: 0}}> <div style={{position:'relative', left:0, right: 0}}>
{visibleItems.map(item => ( {visibleItems.map(item => (
<div className="item" key={item.id} style={{ height: itemHeight }}> <div className="item" key={item.id} style={{ height: itemHeight }}>
{item.name} {item.name}
@@ -32,7 +36,10 @@ function VirtualList({ items, itemHeight=50, containerHeight=400 }) {
</div> </div>
) )
} }
/*
*/
function App() { function App() {
const generatedBigDataSet = () => { const generatedBigDataSet = () => {
return Array.from({ length: 100 }, (_, index) => ({ return Array.from({ length: 100 }, (_, index) => ({
+2 -2
View File
@@ -1,7 +1,7 @@
.container { .container {
display: flex; /* display: flex;
flex-direction: column; flex-direction: column;
flex-wrap: nowrap; flex-wrap: nowrap;*/
overflow: auto; overflow: auto;
border: 1px solid black; border: 1px solid black;
} }