Can anyone come across this or tell me which way to look The problem is that: There is a list of elements (dynamic) and there is a window of a certain size. Is it possible to implement the case that when scrolling this list, the flag would change to true for the elements that are visible.
let arr = [
{ 1: 2, flag: true },
{ 1: 3, flag: false },
{ 1: 4, flag: false },
{ 1: 5, flag: false },
{ 1: 6, flag: false },
{ 1: 7, flag: false },
{ 1: 8, flag: false },
{ 1: 9, flag: false },
{ 1: 10, flag: false },
{ 1: 11, flag: false },
{ 1: 12, flag: false },
{ 1: 13, flag: false },
{ 1: 14, flag: false },
];
return (
<div
style={{
width: '100px',
overflow: 'scroll',
height: '60px',
}}
>
{arr.map((el) => (
<div key={el[1]}>
<div style={{backgroundColor: `${el.flag ? 'green' : 'red'}`, margin: '5px 0'}}>{el[1]}</div>
</div>
))}
</div>
);
There is a link for general views of the problem: https://stackblitz.com/edit/react-ux1rre?file=src/App.js
Thanks for any advice.
2
Answers
Add this part of code to the main div and this will tell you the position on scroll.
From this point you can do the math and know if an item was seen or not by the user.