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

59
Visualizações
Javascript gradually change the saturation

I have some code so that when the key c is pressed, the saturation will increase and when the key c is lifted, it will revert back to normal. I'm wondering how I can make it so that instead of instantly changing the saturation level, the saturation will gradually change throughout the course of half a second and when the key c is lifted, the saturation will gradually decrease throughout a half second back to the normal saturation level. How can this be done? Current code:

document.addEventListener("keydown", e => {
  if (e.code === "KeyC") {
document.body.style.filter = "saturate(1.7)";

    
    
    
    }
}, false)

document.addEventListener("keyup", e => {
  if (e.code === "KeyC") {
  document.body.style.filter = "";

  
  }
}, false)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I would use use a CSS transition to get the gradual change. Then instead of setting the saturate value with javascript you can just do .classList.add('saturated') and .classList.remove('saturated') on the element. I made an example with a div:

#myDiv{
  width:200px;
  height:200px;
  background-color: lightblue;
  filter: saturate(1);
  transition: filter 1s ease;
}

#myDiv.saturated {
  filter: saturate(4);
}

here is a working example on JSFiddle: https://jsfiddle.net/martinnester/07fsL2ge/20/

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