Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
Lazy-loading grid: scroll a large, empty div that is mostly hidden by overflow: auto

I am currently implementing a large, lazily rendered grid, with the goal of being able to display many more rows and columns than you could if you just rendered them directly. To do so, I've implemented a solution where:

  1. The grid is a container div that holds a scroller and content div. The scroller div is empty, and the content div displays whatever is visible on the screen.
  2. The user scrolls in the container by scrolling the scroller.
  3. Based on where the scroller div is, the content div renders different data for the content.
  4. The content div translates itself around so that it stays positioned correctly.

Functionally, this allows us to render very few divs, while from the users perspective, they can scroll through their data as normal. This is necessary, given the data sizes I'm working with.

However, I need to be able to scroll the grid to a specific location (e.g. the 100th row, 3rd column). To do so, I need to be able to move the scroller div. However, as most of scroller div is hidden (by the grid container), I cannot figure out how to use the scroll or scrollBy functions to accomplish this. As the scroller element is mostly hidden, the scroll function cannot move it, as scroll only works in relation to the visible aspect of the element (I think!).

I striped down the grid to the minimal reproducible example in the codepen here: https://codepen.io/naterush/pen/WNOZGze (it was too big to be reasonable to put in this question). This just has a single row of data, and you can scroll to the right inside of it to see more data.

The only code that needs to be finished is the scrollTo20 function. Any thoughts or comments or directions would be greatly appreciated!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem is that Element.scroll() is attached to the wrong element. If you want to scroll the div, consider changing the function to scroll from the outer div, not on a div within a container.

let currNumber = 0;

function onScroll(e) {
  let { scrollLeft, scrollTop } = e.target;
  // First, move the content div to the correct place
  let xOffset = scrollLeft % 200;  document.getElementById('content').style.transform = `translate(${scrollLeft - xOffset}px, 0px)`
  // Then, update the divs inside the content div to have the correct numbers
  let startingNumber = Math.floor(scrollLeft / 200);
  
  if (currNumber != startingNumber) {
    console.log("Changing to", startingNumber)
    for (let i = 0; i < 5; i++) {
      document.getElementById('' + i).innerHTML = startingNumber + i;
  }  
  }
  
  
  currNumber = startingNumber;
}

function scrollTo20() {
  console.log("This should scroll the #scroller so that the div then ends up being displayed starts at 20")
 
  document.getElementById('container').scroll(20 * 200, 0); // point to container instead of scroller
  
}

document.getElementById('container').addEventListener('scroll', onScroll)
* {
  box-sizing: border-box;
}

#app {
  padding: 2rem;
  height: 400px;
  width: 1000px;
}

#container {
  width: 100%;
  height: 200px;
  border: 1px solid black;
  position: relative;
  overflow: auto;
}

#scroller {
  width: 10000px;
  height: 100%;
}

#content {
  top: 0;
  left: 0;
  position: absolute;
}

.row {
  display: flex
}

.row > div {
  width: 200px;
}
<div id='app'> <div id='container'> <div id='scroller'/> <div id='content'> <div class='row'> <div id='0'> 0 </div><div id='1'> 1 </div><div id='2'> 2 </div><div id='3'> 3 </div><div id='4'> 4 </div><div id='5'> 5 </div></div></div></div></div><button onclick='scrollTo20()'> Scroll to 20</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

UPDATE: @testing_22 figured out the solution. The Element.scroll function scrolls within a div, not on a div within a container. I just had to change what I was trying to scroll.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda