Javascript 如何在jQuery中等待所有动画完成?
我有多个动画,我想在所有动画完成后执行我的代码 这段代码的问题在于,它是在第一个动画完成后执行的,而不是等待所有动画完成 有解决办法吗Javascript 如何在jQuery中等待所有动画完成?,javascript,jquery,css,css-animations,Javascript,Jquery,Css,Css Animations,我有多个动画,我想在所有动画完成后执行我的代码 这段代码的问题在于,它是在第一个动画完成后执行的,而不是等待所有动画完成 有解决办法吗 <style> .userClass { animation: show 7s 6s forwards,zoom 8s 6s forwards,hide 5s 13s forwards;} @@-webkit-keyframes show{ 0% { opacity: 1;
<style>
.userClass {
animation: show 7s 6s forwards,zoom 8s 6s forwards,hide 5s 13s forwards;}
@@-webkit-keyframes show{
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@@-webkit-keyframes zoom{
//
}
@@-webkit-keyframes hide{
//
}
</style>
<div class="userClass" id="user"></div>
<script>
$(document).ready(function () {
$("#user").bind("animationend", function () {
setTimeout(function () {
//I want to execute code here
}, 3000);
});
});
</script>
.userClass{
动画:向前显示7s 6s,向前缩放8s 6s,向前隐藏5s 13s;}
@@-webkit关键帧显示{
0% {
不透明度:1;
}
100% {
不透明度:0;
}
}
@@-webkit关键帧缩放{
//
}
@@-webkit关键帧隐藏{
//
}
$(文档).ready(函数(){
$(“#user”).bind(“animationend”,函数(){
setTimeout(函数(){
//我想在这里执行代码
}, 3000);
});
});
动画结束事件将触发三次,先是13秒后,然后是1秒后,第三次是4秒后(总共18秒后)
只需计算一个变量,只处理第三个事件。动画结束事件将触发三次,先是13秒后,然后是1s后,然后是第三次4s后(总共18秒后)
只需计算一个变量,只处理第三个事件。创建一个变量,以跟踪触发次数
animationEnd
第三次触发时,执行代码。大概是这样的:
$(document).ready(function () {
// Keep track of how many animations have ended
var animationEndCount = 0;
$("#user").bind("animationend", function () {
// Increment our counter
animationEndCount++;
// IF our counter is 3, run some code
if (animationEndCount === 3) {
setTimeout(function () {
}, 3000);
}
});
});
创建一个变量来跟踪触发次数
animationEnd
第三次触发时,执行代码。大概是这样的:
$(document).ready(function () {
// Keep track of how many animations have ended
var animationEndCount = 0;
$("#user").bind("animationend", function () {
// Increment our counter
animationEndCount++;
// IF our counter is 3, run some code
if (animationEndCount === 3) {
setTimeout(function () {
}, 3000);
}
});
});
可能重复的
$('.selector')。一个('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend',doSomething)
@BrettGregson这不起作用,它只是检测到第一个动画结束…tnx要重复动画,可以使用removeClass()
删除动画的CSS类,然后使用addClass()
可能重复的$('.selector')再次添加它。一个('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend',doSomething);
@BrettGregson这不起作用,它只是检测到第一个动画结尾…tnx要重复动画,可以使用removeClass()
删除动画的CSS类,然后使用addClass()再次添加它
谢谢你的回答,但无法用更动态的方式解决。我的意思是像在视图中检测所有动画结束一样。谢谢你的回答,但无法用更动态的方式解决。我的意思是像在视图中检测所有动画结束一样。