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

164
Visualizações
Reversing Animejs animation that's applied on click to array of elements?

I have a set of custom checkboxes that I'm applying an Animejs animation to on click. I figured this was the only way to get the animation working for each checkbox without having to manually add event listeners to each checkbox.

I'm having some troubles figuring out how to reverse the animations, however.

For reference, here's a codepen of what I'm working with so far:

https://codepen.io/osaisus/pen/BaJWvPv

HTML:

<p>How can I get the animations to reverse on second click?</p>
<div class="container">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
</div>

CSS:

.container {
  display: flex;
  align-items: center;
}

.box, .box2 {
  border: 1px solid black;
  padding: 10px;
  margin: 1rem 1rem;
}

JS:

const checkbox = document.querySelectorAll(".box");

checkbox.forEach(item => {
  item.addEventListener("click", (e) => {
    e.preventDefault();
    anime({
      targets: item,
      duration: 200,
      easing: "easeInOutQuint",
      borderRadius: [3, 50],
      backgroundColor: "#ffd54f",
      borderColor: ["#212529", "#ffd54f"],
    });
  })
})

There are many other posts out there on how to reverse animations on SINGLE elements, but none of the methods (as far as I've tested) work when applying the animation to a set of elements like what's done above.

Any help is much appreciated!

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

0

Doesn't take too much. Don't have to do it the way I did here, just keep some record of where you've been accessible to your event handler.

const checkbox = document.querySelectorAll(".box");

checkbox.forEach(item => {
  let props = {
    borderRadius: [3, 50],
    backgroundColor: ["rgba(255, 255, 255, 0)", "#ffd54f"],
    borderColor: ["#212529", "#ffd54f"],
  };
  item.addEventListener("click", (e) => {
    e.preventDefault();
    anime({
      targets: item,
      duration: 200,
      easing: "easeInOutQuint",
      ...props
    });
    for (i in props) {
      props[i].reverse();
    }
  })
})

https://codepen.io/different55/pen/oNpdbmq?editors=1111

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