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

88
Visualizações
Temporarily deviate from a CSS animation on hover, then resume

An object's colour is cycling between soft red and blue, using a keyframe animation.

  1. When I hover on it, I want the animation to pause, and the colour turn yellow.
  2. When I move away, it should smoothly transition back to the colour it was when I hovered, and the keyframe colour animation should resume from where it paused.

Minimal straightforward code (Codepen demo):

#box {
  width: 100px;
  height: 100px;
  background-color: #ffcccc;
  animation: cycleColor 2s infinite linear;
  transition: background-color .2s;
}

#box:hover {
  animation-play-state: paused;
  background-color: yellow;
}

@keyframes cycleColor {
  0% {
    background-color: #ffcccc;
  }
  50% {
    background-color: #ccccff;
  }
}
<div id='box'></div>

This doesn't generally work, presumably because a keyframe animation won't allow a temporary change, even when paused.

Instead, I can totally remove the animation on hover, but then it can't smoothly resume from where it paused.

I have also tried adding and removing classes upon hover, but that hasn't solved this problem.

Is there a CSS or JS solution?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Use box-shadow to create an extra layer

#box {
  width: 100px;
  height: 100px;
  background-color: #ffcccc;
  animation: cycleColor 2s infinite linear;
  transition: box-shadow .2s;
}

#box:hover {
  animation-play-state: paused;
  box-shadow: 0 0 0 100vmax inset yellow;
}

@keyframes cycleColor {
  0% {
    background-color: #ffcccc;
  }
  50% {
    background-color: #ccccff;
  }
}
<div id='box'></div>

about 4 years ago · Santiago Trujillo Relatório

0

A bit of a hacky approach:

#box {
  width:100px;
  height:100px;
  background-color: #ffcccc;
  animation: cycleColor 2s infinite linear;
  transition: background-color .2s ease;
  position: relative;
}

#box:hover {
  animation-play-state: paused;
}

#box::after {
  position: absolute;
  width: 100%;
  height: 100%;
  content: '';
  display: block;
  background: yellow;
  opacity: 0;
  transition: opacity 0.3s ease;
}

#box:hover::after {
  opacity: 1;
}

@keyframes cycleColor {
    0% {
        background-color: #ffcccc;
    }    50% {
        background-color: #ccccff;
    }
}
<div id='box'></div>

about 4 years ago · Santiago Trujillo 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