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

122
Visualizações
Unintended CSS Reversing Animation

I'm trying to make an icon button to rotate on click, but every time I remove the class, it spins backwards, I wanted to do in a way that every time it's clicked, it only spins clockwise.

const btn = document.querySelector('.reset-button');

btn.addEventListener("click", (e) => {
  btn.classList.add("rotate");
  setTimeout(() => {
    btn.classList.remove("rotate");
  }, 1500);
});
div {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.reset-button {
  transition: 1.5s;
}

.rotate{
  transform: rotate(720deg);
}
<link rel="stylesheet"
        href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />
        
        <div>
        <button class="reset-button rotate"><span class="material-symbols-outlined">
                            refresh
                        </span></button>
                        </div>

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

0

You want the transition effect to occur only as the element transitions from 0 rotation to 720deg.

In the other direction (720deg to 0deg) you want it to happen instantly so the user sees no change.

Remove the transition from where it is and have it only on the rotating class.

const btn = document.querySelector('.reset-button');

btn.addEventListener("click", (e) => {
  btn.classList.add("rotate");
  setTimeout(() => {
    btn.classList.remove("rotate");
  }, 1500);
});
div {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.reset-button {}

.rotate {
  transform: rotate(720deg);
  transition: 1.5s;
}
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />

<div>
  <button class="reset-button rotate"><span class="material-symbols-outlined">
                            refresh
                        </span></button>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

While this might be on purpose, notice that besides for rotating back, your code doesn't rotate the button at all the first time it's clicked after the page is loaded.

Here's an alternate option (not the only way to do this) that rotates the button even when you click on it for the first time after the page was loaded in case that is of use to anyone.

const btn = document.querySelector('.reset-button');

btn.addEventListener("click", (e) => {
    btn.style.transition = '1.5s';
  btn.style.transform = 'rotate(720deg)';
  setTimeout(() => {
    btn.style.transition = '0s';
    btn.style.transform = 'rotate(0deg)';
  }, 1500);
  
});
div {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.reset-button {}
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />

<div>
  <button class="reset-button rotate"><span class="material-symbols-outlined">
                            refresh
                        </span></button>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Only have the transition CSS property to the rotate class and so the animation will occur when rotate class is added, since the transition property was added in the reset-button class, the animation was happening at both times as when rotate class was added and removed.

Also initially remove the rotate class from HTML.

    const btn = document.querySelector('.reset-button');

    btn.addEventListener("click", (e) => {
      btn.classList.add("rotate");
      setTimeout(() => {
        btn.classList.remove("rotate");
      }, 1500);
    });
    div {
      display: flex;
      flex-direction: row;
      justify-content: center;
    }

    .reset-button {
      
    }

    .rotate{
      transition: 1.5s;
      transform: rotate(720deg);
    }

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