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

315
Visualizações
Remove in animation() doesn't remove

I have a div id'ed #scenario which has a number of <p> elements as children. I want to have a function that fades them out one by one in reverse order and then removes them from the DOM. I'm using jQuery.

My code:

function fadeAndDelete() {
  var elements = $("#scenario").children().get().reverse();
  $.each(
    elements, function(index, element) {
      element.animate(
        {'color': 'rgba(0, 0, 0, 0)'}, 1000, function() {
          $(this).remove();
        }
      );
    }
  )
};

I also tried to use element.remove() and $(element).remove(), and none of them worked.

Bonus points: in an ideal world, there would be a time-gap between the removals of the element (as if using a sleep() function inbetween the each() cycles).

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

0

The animate() you're using is the DOM animate, not jquery's:

https://developer.mozilla.org/en-US/docs/Web/API/Element/animate

animate(keyframes, options)

There's no callback parameter for this function, so it's not that .remove() doesn't work, it's that the function is never called.

To use jquery's animate you need a jquery object:

$.each(elements, function(index, element) {
      $(element).animate(...

However, note that jquery is notorious for not being able animate colours without an additional plugin or using css, so your code will then not animate, but the .remove() will be called.


For the one-at-a-time scenario, you can use setTimeout. There's plenty of existing answers on SO for this, but here's your code:

function fadeAndDelete() {
  var elements = $("#scenario").children().get().reverse();

  $.each(elements, function(index, element) {
    setTimeout(function() { 
      $(element).fadeOut(2000, function() { 
        this.remove(); 
      }); 
    }, index*500);
  });
};

fadeAndDelete();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id=scenario>
  <div>a</div>
  <div>b</div>
  <div>c</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