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

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 './App.scss'
import { useEffect, useRef, useState } from 'react';
import styles from './App.css';
function VirtualList({ items, itemHeight=50, containerHeight=400 }) {
const containerRef = useRef(null)
//Положение большого нивидимого контейнера
const [scrollTop,setScrollTop] = useState(0);
//Количество отображаемых элементов
const visibleCount = Math.ceil(containerHeight / itemHeight);
//Общая высота нивидимого контейнера
const totalHeight = items.length * itemHeight;
//Начало и конец подгружаемых(отображаемых) элементов
const startIndex = Math.floor(scrollTop / itemHeight);
const endIndex = Math.min(startIndex + visibleCount + 1, items.length);
//Срез видимых элементов из общего списка
const visibleItems = items.slice(startIndex,endIndex);
const handleScroll = e => {
setScrollTop(e.target.scrollTop);
console.log(e.target.scrollTop)
}
return (
<div ref={containerRef} className="container" style={{
height: containerHeight
}} onScroll={handleScroll}>
<div style={{ height:totalHeight }}>
<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>
)
}
/*
const InfiniteList = () => {
const [items, setItems] = useState([...Array(20).keys()]); // Initial data
const loaderRef = useRef(null);
const loadMore = () => {
setItems((prev) => [...prev, ...Array(10).keys()].map((_, i) => prev.length + i));
};
useEffect(() => {
const observer = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting) {
loadMore(); // Load next batch when the loader is seen
}
});
if (loaderRef.current) observer.observe(loaderRef.current);
return () => observer.disconnect();
}, []);
return (
<ul>
{items.map(item => <li key={item} className={ styles.item }>Item {item}</li>)}
<div ref={loaderRef}>Loading more...</div>
</ul>
);
};
*/
function App() {
const generatedBigDataSet = () => {
return Array.from({ length: 100 }, (_, index) => ({
@@ -54,7 +42,9 @@ function App() {
<>
<div>
<h2>Визуализация большого списка</h2>
<VirtualList items={data} itemHeight={60} containerHeight={500} />
<div className={ styles.container }>
<InfiniteList />
</div>
</div>
</>
)
+1
View File
@@ -2,6 +2,7 @@
/* display: flex;
flex-direction: column;
flex-wrap: nowrap;*/
height: 500px;
overflow: auto;
border: 1px solid black;
}