O meu fadeOut está ok, mas o fadeIn nao está a funcionar...
function fadeIn(elem, speed)
{
if(!elem.style.opacity)
{
elem.style.opacity = 0;
}
InInterval(function(){
elem.style.opacity += 0.03;
}, speed /100);
}
Stack Overflow em Português é um site de perguntas e respostas para programadores profissionais e entusiastas. Leva apenas um minuto para se inscrever.
Inscreva-se para participar desta comunidadeO meu fadeOut está ok, mas o fadeIn nao está a funcionar...
function fadeIn(elem, speed)
{
if(!elem.style.opacity)
{
elem.style.opacity = 0;
}
InInterval(function(){
elem.style.opacity += 0.03;
}, speed /100);
}
O problema é que o valor da opacidade é interpretada como uma string (texto), e não como um número. Podes fazer assim:
function fadeIn(elem, speed) {
if(!elem.style.opacity) {
elem.style.opacity = 0;
}
timer = setInterval(function(){
if(parseFloat(elem.style.opacity) >= 1) clearInterval(timer);
elem.style.opacity = parseFloat(elem.style.opacity) + 0.03;
}, speed / 100);
}
ele = document.getElementById('hey');
fadeIn(ele, 5000);
#hey {
background-color: red;
width: 100px;
height: 100px;
}
<div id="hey">
</div>