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

285
Visualizações
Loop fadeIn and fadeout every 2 items in javascript, jquery

i would like to ask how to make loop fadeIn and fadeout every 2 items from unorder list. I trying to do this with jQuery but my loop only take 1 by 1 item.

<div>
  <ul>
   <li>item1</li> // will be first display
   <li>item2</li> // will be first display
   <li>item3</li> // will be second display
   <li>item4</li> // will be second display
   <li>item5</li> // will be third display
  </ul>
</div>

My javaScript code. I try to split array for 2 elements each array then im trying to fade out it.

function slide(elements) {

var perChunk = 2

var inputArray = Array.from(elements[0].children);

var result = inputArray.reduce((resultArray, item, index) => { 
  const chunkIndex = Math.floor(index/perChunk)

  if(!resultArray[chunkIndex]) {
    resultArray[chunkIndex] = [] 
  }

  resultArray[chunkIndex].push(item)

  return resultArray
}, [])

console.log('res',result);

$(inputArray).hide();


    setInterval(function() { 
        result.map(el => {

        $(el).show().fadeIn();
        });
    }, 2000);
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Using .slice() you can select a range of elements, then each time increase the start of that range.

With the help of this question to call a function when all elements have finished .fadeOut() (otherwise you get a callback per element), gives:

var items = $("ul>li");
var pos = 0;
var count = 2;

items.hide().slice(pos, pos+count).show();
setInterval(() => {
  $.when(items.slice(pos, pos+count).fadeOut())
    .then(function() {
      pos += count;
      if (pos>items.length) 
        pos = 0;
      items.slice(pos, pos+count).fadeIn();
    });
}, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <ul>
   <li>item1</li> 
   <li>item2</li> 
   <li>item3</li> 
   <li>item4</li> 
   <li>item5</li> 
  </ul>
</div>

There are, of course, many different ways to do this, for example using $("ul>li").each((idx) => and comparing the idx (index) value or using a setTimeout instead of the $.when to fadeIn when fadeOut has completed.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you are just doing two list items together each time:

In your for loop increment i by 2 each time and set i and i+1 to visible, fadein and the rest to hidden, fadeout. With your example have 5 items, I would put a check to make sure the i+1 is not greater than your set list length.

for(i=0;i<list_length;i+=2){
  //Add fade out ability here if you want it to happen before the fadein    
  $(list[i]).fadin();
  $(list[i+1]).fadein();
  //Add fade out ability here if you want it to happen after the fadein
}

If you are no just make every other item in your list fade in and out. Set an id for the ones you want to pair together, then loop over your list and fadein and fadeout which ones want active at a time. If this is the option you are looking for I can add some example code but I think you are looking for the first option.

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