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

216
Visualizações
Preserve scroll position when removing elements

There is a container with some elements that can be removed from it. If you scroll to the end of the container and remove the last element, the container will automatically "shrink" and move up.

function remove(target) {
    target.remove();
}
body {
  margin: 0;
}
.container {
  overflow-y: scroll;
  height: 200px;
  background-color: gray;
}

.el {
  height: 60px;
  background-color: cadetblue;
  margin: 5px;
  text-align: center;
  vertical-align: middle;
  line-height: 60px; 
  font-size: 20pt;
}
<div class="container fill">
  <div class="el" onclick="remove(this)">1</div>
  <div class="el" onclick="remove(this)">2</div>
  <div class="el" onclick="remove(this)">3</div>
  <div class="el" onclick="remove(this)">4</div>
  <div class="el" onclick="remove(this)">5</div>
  <div class="el" onclick="remove(this)">6</div>
  <div class="el" onclick="remove(this)">7</div>
</div>

What I want: when any element is removed from the container, the container will somehow extend its inner content height in such a way that it will not change its scroll position (i.e. all the elements above the removed one do not change their position). However, I do not want this container to have an extra space at the end defined by CSS height property. I want this space appear only when needed, i.e. when this space becomes invisible after scrolling up, it is impossible to scroll there back anymore.

How can I achieve this with pure HTML5, CSS and JS?

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

0

So you want the container adjusts the height of the content if there are not many items left? Then you could use max-height: 200px; instead of height: 200px;.

function remove(target) {
    target.remove();
}
body {
  margin: 0;
}
.container {
  overflow-y: scroll;
  max-height: 200px;
  background-color: gray;
}

.el {
  height: 60px;
  background-color: cadetblue;
  margin: 5px;
  text-align: center;
  vertical-align: middle;
  line-height: 60px; 
  font-size: 20pt;
}
<div class="container fill">
  <div class="el" onclick="remove(this)">1</div>
  <div class="el" onclick="remove(this)">2</div>
  <div class="el" onclick="remove(this)">3</div>
  <div class="el" onclick="remove(this)">4</div>
  <div class="el" onclick="remove(this)">5</div>
  <div class="el" onclick="remove(this)">6</div>
  <div class="el" onclick="remove(this)">7</div>
</div>

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