I have a fade animation I made in JavaScript but the numbers seem to become NaN
and I cannot work out where I went wrong.
My fade animation is in this function:
function transition_opacity(div_id,opacity,direction,fnc){
var opacity = parseFloat(opacity);
var IntervId = setInterval(process_transition,30);
function process_transition(){
console.log(opacity); //check value as it runs
if(direction){
opacity = opacity + 0.1; //fade back in
} else {
opacity = opacity - 0.1; //fade to transparency
}
div_id.style.opacity = opacity;
if(!direction && opacity < 0.0 || direction && opacity > 1){
clear();
}
}
function clear(){
clearInterval(IntervId);
if(fnc){ fnc(); }
}
}
The fade to transparency works fine but fading back up is where it goes wrong... i call the function like this:
var div_id='test';
function display(){
var opacity = window.getComputedStyle(div_id).opacity;
transition_opacity(div_id,opacity,1,0); //fade in
}
var opacity = window.getComputedStyle(div_id).opacity;
transition_opacity(div_id,opacity,0,load); //fade out
The issue is on fade in, the opacity value is not a number from the very start. I don't understand why that is the case when it faded out perfectly fine so surely the opacity should be 0.0?