Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are javascript and css timing synchronized?

Are javascript (timeout, interval) and css (animations, delay) timing synchronized ?

For instance :

#anim1 {
animation: anim1 10s linear;
display: none;
}

anim1.style.display = "block" ;
setTimeout(function() {
 anim2.style.webkitAnimation= 'anim2 10s linear';
}, 10000);

Will anim2 be precisely triggered at the end of anim1 ? Is it different depending on the browser ? In this case I'm more interested in a webkit focus.

Note that anim1 is triggered via javascript to avoid loading time inconsistencies.

NB : This is a theoretical question, the above code is an illustration and you must not use it at home as there are way more proper means to do so.

like image 428
kursus Avatar asked Jun 09 '13 14:06

kursus


People also ask

Is JavaScript slower than CSS?

CSS has fairly good performance as it offloads animation logic onto the browser itself. This lets the browser optimize DOM interaction and memory consumption and most importantly, uses the GPU to improve performance. On the other hand, Javascript performance can range from reasonably faster to much slower than CSS.

Is CSS more performant than JS?

The fact is that, in most cases, the performance of CSS-based animations is almost the same as JavaScripted animations — in Firefox at least. Some JavaScript-based animation libraries, like GSAP and Velocity. JS, even claim that they are able to achieve better performance than native CSS transitions/animations.

Do CSS animations affect performance?

TL;DR # Take care that your animations don't cause performance issues; ensure that you know the impact of animating a given CSS property. Animating properties that change the geometry of the page (layout) or cause painting are particularly expensive. Where you can, stick to changing transforms and opacity.

Is it better to animate with CSS or JavaScript?

TL;DR # Use CSS animations for simpler "one-shot" transitions, like toggling UI element states. Use JavaScript animations when you want to have advanced effects like bouncing, stop, pause, rewind, or slow down.


2 Answers

As far as I know, there is no guarantee. However, there are events which you can listen for;

anim1.addEventListener('animationend',function(){
    anim2.style.webkitAnimation= 'anim2 10s linear';
}

Note that because these are new, there are still vendor prefixes you need to account for; webkitAnimationEnd and oanimationend for Webkit and Opera.

Also as my original answer (wrongly) suggested, there is transitionend (with similar prefixes) if you want to use CSS transitions instead of animations.

like image 100
Dave Avatar answered Oct 06 '22 23:10

Dave


This is the wrong way to do this. There isn't any guarantee that they will be in sync, though it's likely they'll be close.

Events are provided for the start, end an repeat of an animation.

https://developer.apple.com/documentation/webkitjs/webkitanimationevent details these.

Use code like:

element.addEventListener("webkitAnimationEnd", callfunction,false);

to bind to it.

like image 23
Rich Bradshaw Avatar answered Oct 06 '22 22:10

Rich Bradshaw