Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
javascript onclick css fade in each time, not just 1st time

How can I have text fade in on each click and not just the first time using css transition and JavaScript?

Here is what I have so far

<style>
#data {
    transition: .7s;
}
</style>

<h1 id="data"></h1>
<a href="#" id="clicker">click 1</a>

document.getElementById('data').style.opacity = 0;
function go(event){
    event.preventDefault();
    document.getElementById('data').style.opacity = 1;
    document.getElementById('data').innerHTML = 'test';
}
document.getElementById('clicker').addEventListener('click', go);
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

It can also be done using Element.animate():

const dataElement = document.getElementById('data')
dataElement.style.opacity = 0

function go(event) {
  event.preventDefault()
  dataElement.animate({
    opacity: [0, 1]
  }, 700).onfinish = () => dataElement.style.opacity = 1
}
document.getElementById('clicker').addEventListener('click', go)
<h1 class="fade-in" id="data">test</h1>
<a href="#" id="clicker">click 1</a>

EDIT: In the above snippet onfinish event handler was used to maintain the final opacity value since it was being set back to 0 after the animation ends. But I found that this can also be achieved by setting fill: 'forwards' in the keyframe options:

const dataElement = document.getElementById('data')
dataElement.style.opacity = 0

function go(event) {
  event.preventDefault()
  dataElement.animate({
    opacity: [0, 1]
  }, {
    duration: 700,
    fill: 'forwards'
  })
}
document.getElementById('clicker').addEventListener('click', go)
<h1 class="fade-in" id="data">test</h1>
<a href="#" id="clicker">click 1</a>

Also you might want to check browser compatibility before implementing those approaches

And if you want a safer approach you may use css animations:

const data = document.getElementById('data')
data.style.opacity = 0
const clicker = document.getElementById('clicker')
clicker.addEventListener('click', () => {
  data.classList.remove('fade-in')
  data.offsetWidth // required to trigger a reflow and restart the animation
  data.classList.add('fade-in')
})
.fade-in {
  animation-name: fadein-animation;
  animation-duration: 700ms;
  animation-fill-mode: forwards;
}

@keyframes fadein-animation {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
<h1 id="data">test</h1>
<a href="#" id="clicker">click 1</a>

about 4 years ago · Juan Pablo Isaza Denunciar

0

setTimeout might work for you. On click set it invisible immediately and use setTimeout to have a delay then show it again.

document.getElementById('data').style.opacity = 0;
document.getElementById('data').innerHTML = 'test';
function go(event){
    event.preventDefault();
    document.getElementById('data').style.opacity = 0;
    setTimeout(() => {
      document.getElementById('data').style.opacity = 1;
    }, 1000);
}
document.getElementById('clicker').addEventListener('click', go);
#data {
    transition: .7s;
}
<h1 id="data"></h1>
<a href="#" id="clicker">click 1</a>

about 4 years ago · Juan Pablo Isaza Denunciar

0

<h1 id="data" style="opacity:0">&nbsp;</h1>
<button type="button" id="clicker">Fade In</button> 
<script>
var data = document.getElementById('data');
function fadeIn(){
    data.innerHTML = 'Data entered successfully.';
    data.animate({opacity:[0,1]},{duration:400,fill:'forwards'});
}
document.getElementById('clicker').addEventListener('click',fadeIn);
</script>

only IE doesn't not support animate

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda