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

263
Visualizações
JS/CSS: removing flexbox last row orphan items by using a resize event listener to show/hide elements?

I have a flex grid that show items of different widths (rectangle, square, etc) and it pulls data from an API so the list is dynamic so each row is always different. This often results in "orphan" flex items at the end of the flex grid, especially at lower breakpoints.

I've noticed I usually only get 2 or 3 items lingering in the last row.

My goal is to only show full rows and get rid of all orphan items.

enter image description here

I thought of different ways to accomplish this and I noticed that all my orphan items are both 1) occurring when I shrink my screen below 1750px and 2) the orphan items are above 300px, large since they have no content next time to them that forces them to be small.

I thought I would add a resize event listener to check check only the last 6 items of my flex grid. If it's height is above 310, I add the style display: none , otherwise display: block.

this.throttledCheckOrphanItems = throttle(() => this.checkOrphanItems(), 300);
window.addEventListener('resize', this.throttledCheckOrphanItems);

checkOrphanItems() {
  if (window.innerWidth > 1750) {
    return;
  }
  const items = document.getQuerySelectorAll('.movieItems');

  for (let x = items.length - 1; x >= items.length - 6; x--) {
    const height = items[x].getBoundingClientRect().height;
    if (height >= 310) {
      items[x].style.display = 'none';
    } else {
      items[x].style.display = 'block';
    }
  }
},

It "works" but it is not a smooth experience.

I know this isn't a great solution but I can't find another way without sacrificing the setup of my grid. Is there a better way I can only show full rows? If not, is there a way to optimize my solution or make it more performant?

about 4 years ago · Juan Pablo Isaza
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