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

583
Visualizações
how to know opacity value using javascript?

I am using transition: opacity 5s; property. I want to show different alert or console message when my opacity value is 0.4 or 0.6 or .2 . on button click I am doing transition but I want to know opacity progress so that i will show those message ?

is there any way to do this

var btn = document.querySelector("button");
var par = document.querySelector("#parId");
btn.addEventListener("click", (e) => {
  par.classList.add("removed");
});
par.addEventListener("transitionend", () => {
  par.remove();
});


#parId {
  transition: opacity 5s;
}
.removed {
  opacity: 0;
}

we are getting transitionend callback if there any progress callback where I will check opacity value ?

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

0

You could potentially check periodically like this, although your interval will need to be at least the speed of the opacity animation or be quicker than it to catch the values.

var par = document.querySelector("#parId");
    
     setInterval(function() {
      console.log(window.getComputedStyle(par).opacity);
     }, 100)
#parId{
opacity: 0.2;
  transition: opacity 3s ease-in-out;
  }
  
  
#parId:hover {
  opacity: 1;
}
<div id="parId">
test
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

There is no event that can be listened to to give what you want - unless you are going to use a linear transition. In that case you can carve your changes of opacity up into 0.2s slots, changing opacity on transitionend to the next value down - 0.8, 0.6 etc.

Your code however takes the default for the transition-timing-function property which is ease - not linear - so transitionend is of no use to you.

This snippet polls the opacity changes every tenth of a second and writes the current opacity to the console so you can see what is happening.

A couple of points: you will have to check for when the opacity goes just less than one of your break points, you are unlikely every to hit it just at exactly 0.6s or whatever; also notice that the console carries on being written to after the element has totally disappeared. The timing will not be exact, things are happening asynchronously.

<style>
#parId {
  transition: opacity 5s;
  width: 50vw;
  height: 50vh;
  background: blue;
  opacity: 1;
  display: inline-block;
}
.removed {
  opacity: 0;
}
</style>
<div id="parId"></div>
<button>Click me</div>
<script>
var btn = document.querySelector("button");
var par = document.querySelector("#parId");
btn.addEventListener("click", (e) => {
 let interval = setInterval(function () {
    const opacity = window.getComputedStyle(par).opacity
    console.log(opacity);
    if (opacity == 0) {clearInterval(interval);}
    }, 100);
    par.style.opacity = 0;
});
</script>

about 4 years ago · Juan Pablo Isaza Relatório

0

Take a look in this example https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/animationend_event

You could define your animation stages as diferent ranimations on css then call them in chain via javascript. Before, you must set an event listener for the animationend event, and every time the event is fired you check the #parId opacity. You could do it.with jQuery to, totaly in javascript

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