Возникла проблема со стилями

This commit is contained in:
2026-05-17 13:11:25 +03:00
parent 50becd2dca
commit 09b3da5f18
3 changed files with 47 additions and 41 deletions
+15
View File
@@ -0,0 +1,15 @@
.container {
/* display: flex;
flex-direction: column;
flex-wrap: nowrap;*/
height: 500px;
overflow: auto;
border: 1px solid black;
}
.item {
font-size: 16pt;
border: 1px dotted gray;
/*margin: 5px 10px;
padding: 10px;*/
border-radius: 5px;
}
+31 -41
View File
@@ -1,45 +1,33 @@
import { useState, useRef } from 'react' import { useEffect, useRef, useState } from 'react';
import './App.scss' import styles from './App.css';
function VirtualList({ items, itemHeight=50, containerHeight=400 }) { const InfiniteList = () => {
const containerRef = useRef(null) const [items, setItems] = useState([...Array(20).keys()]); // Initial data
//Положение большого нивидимого контейнера const loaderRef = useRef(null);
const [scrollTop,setScrollTop] = useState(0);
//Количество отображаемых элементов const loadMore = () => {
const visibleCount = Math.ceil(containerHeight / itemHeight); setItems((prev) => [...prev, ...Array(10).keys()].map((_, i) => prev.length + i));
//Общая высота нивидимого контейнера };
const totalHeight = items.length * itemHeight;
//Начало и конец подгружаемых(отображаемых) элементов useEffect(() => {
const startIndex = Math.floor(scrollTop / itemHeight); const observer = new IntersectionObserver((entries) => {
const endIndex = Math.min(startIndex + visibleCount + 1, items.length); if (entries[0].isIntersecting) {
//Срез видимых элементов из общего списка loadMore(); // Load next batch when the loader is seen
const visibleItems = items.slice(startIndex,endIndex); }
});
const handleScroll = e => {
setScrollTop(e.target.scrollTop); if (loaderRef.current) observer.observe(loaderRef.current);
console.log(e.target.scrollTop) return () => observer.disconnect();
} }, []);
return ( return (
<div ref={containerRef} className="container" style={{ <ul>
height: containerHeight {items.map(item => <li key={item} className={ styles.item }>Item {item}</li>)}
}} onScroll={handleScroll}> <div ref={loaderRef}>Loading more...</div>
<div style={{ height:totalHeight }}> </ul>
<div style={{position:'relative', left:0, right: 0}}> );
{visibleItems.map(item => ( };
<div className="item" key={item.id} style={{ height: itemHeight }}>
{item.name}
</div>
))}
</div>
</div>
</div>
)
}
/*
*/
function App() { function App() {
const generatedBigDataSet = () => { const generatedBigDataSet = () => {
return Array.from({ length: 100 }, (_, index) => ({ return Array.from({ length: 100 }, (_, index) => ({
@@ -54,7 +42,9 @@ function App() {
<> <>
<div> <div>
<h2>Визуализация большого списка</h2> <h2>Визуализация большого списка</h2>
<VirtualList items={data} itemHeight={60} containerHeight={500} /> <div className={ styles.container }>
<InfiniteList />
</div>
</div> </div>
</> </>
) )
+1
View File
@@ -2,6 +2,7 @@
/* display: flex; /* display: flex;
flex-direction: column; flex-direction: column;
flex-wrap: nowrap;*/ flex-wrap: nowrap;*/
height: 500px;
overflow: auto; overflow: auto;
border: 1px solid black; border: 1px solid black;
} }