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

226
Visualizações
How can I show a heading element on button click?

I am using keyframes but my h1 is not visible on button click. What am I doing wrong?

Here is my code:

function abc() {
  document.querySelector('.h1cls').classList.add('fadeIn')
}
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.fadeIn {
  animation-name: fadeIn;
  animation-duration: 150ms;
  animation-timing-function: linear;
  animation-delay: 5000ms;
  /*opacity:1;*/
}

.one .two .h1cls {
  opacity: 0;
}
<div class="one">
  <div class="two">
    <h1 class="h1cls">hello</h1>
    <div></div>
  </div>
</div>

<button onclick="abc()">asd</button>

https://codesandbox.io/s/serene-lamport-12kgz?file=/index.html:0-994

actually I am using opacity . I am setting opacity to 1 after button click.

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

0

In your abc() function, you should also remove h1cls class due to the fact that it still applies opacity: 0 style.

 function abc(){
    document.querySelector('.h1cls').classList.add('fadeIn')
    document.querySelector('.h1cls').classList.remove('h1cls')
 }
about 4 years ago · Juan Pablo Isaza Relatório

0

It is animating but then disappearing immediately. You need to tell the system to keep the final state of the animation. You do this with animation-fill-mode: forwards

Note that the animation will only run on the first click of the button.

about 4 years ago · Juan Pablo Isaza Relatório

0

You have to add animation-fill-mode:forwards; property too in the fadeClass in order to make this work

function abc() {
  document.querySelector('.h1cls').classList.add('fadeIn')
}
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.fadeIn {
  animation-name: fadeIn;
  animation-duration: 150ms;
  animation-timing-function: linear;
  /*animation-delay: 5000ms;*/
  animation-fill-mode:forwards; // add this line
  /*opacity:1;*/
}

.one .two .h1cls {
  opacity: 0;
}
<div class="one">
  <div class="two">

    <h1 class="h1cls">hello</h1>
    <div></div>

    <button onclick="abc()">asd</button>

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