Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
Simple Intifite Scroll in div (React)

I have an array with elements that I want to present in a div with height 150px with an infinite scroll.

I have looked into the Intersection Observer, but I cannot find anything I can use, since my use case is so simple. The elements I want to introduce an infinite scroll look like this:

const data = ["honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota"]


<Container>
    <ul>
      {data.map((d, index) => (
        <li key={`${index}`}>
          <div>
                <span>some text</span>
                <span>some text</span>
                <span>some text</span>
          </div>
        </li>
      ))}
    </ul>
</Container>

I would like to have the infinite scroll inside the Container div.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

(function () {
    const containerEl = document.querySelector('container');
    const itemsEl = document.querySelector('.items');
    const loaderEl = document.querySelector('.loader');
    const items = ["honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab", "toyota","honda", "vw","opel", "saab"];
    const getItems = (page,limit) => {
       const data = {'data':items.slice((page - 1)*limit,page*limit), 'total':items.length}
       return data;
    }
    const showItems = (items) => {
        items.forEach(item => {
            const itemEl = document.createElement('item');
            itemEl.classList.add('item');
            itemEl.innerHTML = `${item} `;
            itemsEl.appendChild(itemEl);
        });
    };
    const hasMore = (page, limit, total) => {
        const startIndex = (page - 1) * limit + 1;
        return total === 0 || startIndex < total;
    };
    const loadItem =  (page, limit) => {
      if (hasMore(page, limit, total)) {
        const fetched=getItems(page, limit);
        showItems(fetched['data']);
        total = fetched['total'];
      }
    };
    let currentPage = 1;
    let limit = 6;
    let total = 0;
    containerEl.addEventListener('scroll', () => {
        const {
            scrollTop,
            scrollHeight,
            clientHeight
        } = containerEl;
        if (scrollTop + clientHeight >= scrollHeight - 5 &&
            hasMore(currentPage, limit, total)) {
            currentPage++;
            loadItem(currentPage, limit);
        }
    }, {
        passive: true
    });
    loadItem(currentPage, limit);
})();
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
container {
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height:150px;
    background:#999999;
    overflow:scroll;
}
.items {
    display: flex;
    flex-direction: column;
    justify-content: center;
}
.item {
    position: relative;
    font-size: 20px;
    line-height: 1.7em;
}
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Infinite Scroll</title>
</head>
<body>
<Container>
    <div class="items"></div>
</Container>
</body>
</html>

Using this nice tutorial: JavaScript Infinite Scroll

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda