简体   繁体   English

javascript和css时序是否同步?

[英]Are javascript and css timing synchronized?

Are javascript (timeout, interval) and css (animations, delay) timing synchronized ? javascript(超时,间隔)和css(动画,延迟)时间是否同步?

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 ? anim2会在anim1结束时被精确触发吗? Is it different depending on the browser ? 是否因浏览器而异? In this case I'm more interested in a webkit focus. 在这种情况下,我对webkit焦点更感兴趣。

Note that anim1 is triggered via javascript to avoid loading time inconsistencies. 请注意,anim1是通过javascript触发的,以避免加载时间不一致。

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. 注意 :这是一个理论问题,上面的代码是一个例子,你不能在家里使用它,因为有更多适当的方法来做到这一点。

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. Webkit和Opera的webkitAnimationEndoanimationend

Also as my original answer (wrongly) suggested, there is transitionend (with similar prefixes) if you want to use CSS transitions instead of animations. 另外,正如我的原始答案(错误地)所暗示的那样,如果您想使用CSS过渡而不是动画,则会有transitionend (具有相似的前缀)。

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. https://developer.apple.com/documentation/webkitjs/webkitanimationevent详细介绍了这些内容。

Use code like: 使用如下代码:

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

to bind to it. 绑定它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM