I am making a function to animate elements in the DOM. Everything works, except for one thing.
Transitions are only working while the element has the css property that has to be animated. For example, I want to slowly fade out an element (opacity 1 to opacity 0). This works only of I apply the opacity property manually. So:
<div class="header-logo" id="id" style="opacity: 1;">Logo</div>
works with the following code:
animate(css) {
if(this !== undefined && css) {
this.css = css;
}
let start = performance.now();
requestAnimationFrame(function animate(time) {
let timeFraction = (time - start) / this.duration;
if( timeFraction > 1) timeFraction = 1;
let timing = this.timer(timeFraction); //Different timers for different easing (like ease-in-out)
this.draw(timing);
if (timeFraction < 1) {
requestAnimationFrame(animate.bind(this));
}
}.bind(this));
}
draw(timing) {
for(let property in this.css) {
this.element.style[property] -= timing * (this.element.style[property] - this.css[property]);
}
}
But if I don't apply the opacity: 1;
, the code doesn't animate.
My concrete question
How can I check whether an element has the to be animated style property? And if it doesn't have the property, how can I apply (dynamically) the to be animated property (so without setting it by hand in the css sheet or inline)?