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

137
Visualizações
Javascript keydown won't call function twice on same key, must alternate keys, simple animation

Anybody know to resolve this?

I just want to do a simple animation using js/css where the object starts in the middle of the screen. Upon right or left arrow keypress, the object then moves to either side of the screen for a few seconds and then returns to the middle of the screen.

For some reason I can hit right and it goes right and returns to the middle and left goes left and returns to the middle.

However, when I hit left twice in a row or right twice in a row, it won't trigger on the second keypress.

document.addEventListener('keydown', keyDownHandler, false); 
  function keyDownHandler(event) {
    if(event.keyCode == 39) {
        falcon.style.animation = "move_right 3s";
    }
    else if (event.keyCode == 37) {
        falcon.style.animation = "move_left 3s";
    }
}

CSS:

@keyframes move_right {
    0% { left: 40%;}
    50% { left: 80%;}
    100% {left: 40%;}
}

@keyframes move_left {
    0% { left: 40%;}
    50% { left: 0%;}
    100% { left: 40%;}
}
about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

The issue you are having in your logic is that your key down event is adding a style to cause the animation. When you click on the same key the same styling is applied thus no change. It works fine when you toggle because the style is updated. A quick fix would be to use a setTimeout function to remove the style after the animation completes, or to clear the styles then add them each time to reset the animation.

You can do this to clear the styles:

document.addEventListener('keydown', keyDownHandler, false); 
  function keyDownHandler(event) {
    if(event.keyCode == 39) {
        falcon.style.animation = "move_right 3s";
        setTimeout(()=>{falcon.style.animation = "" },3000}
    }
    else if (event.keyCode == 37) {
        falcon.style.animation = "move_left 3s";
        setTimeout(()=>{falcon.style.animation = "" },3000}
    }
}
about 4 years ago · Santiago Gelvez Relatório

0

You are setting the animation then setting it again, but the system doesn't rerun it because there has been no change to the style.

You can listen for the animationend event and clear the animation style when it finishes.

Here's a simple snippet:

function clearAnimation() {
  falcon.style.animation = "";
}
const falcon = document.querySelector('.falcon');
document.addEventListener('animationend', clearAnimation);
document.addEventListener('keydown', keyDownHandler, false);

function keyDownHandler(event) {
  if (event.keyCode == '39') {
    falcon.style.animation = "move_right 3s";
  } else if (event.keyCode == 37) {
    falcon.style.animation = "move_left 3s";
  }
}
.falcon {
  position: relative;
}

@keyframes move_right {
  0% {
    left: 40%;
  }
  50% {
    left: 80%;
  }
  100% {
    left: 40%;
  }
}

@keyframes move_left {
  0% {
    left: 40%;
  }
  50% {
    left: 0%;
  }
  100% {
    left: 40%;
  }
}
<div class="falcon">FALCON</div>

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