当我使用它时:
$("#passwordConfig").removeClass('shake animated').addClass('shake animate');
这只有效一次。
我需要添加一些 timeOut 以使其运行任意次数!
$("#passwordConfig").removeClass('shake animated');
setTimeout(function() {
$("#passwordConfig").addClass('shake animated');
}, 50);
知道为什么吗?
CSS:
.animated {
-webkit-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
@-webkit-keyframes shake {
0%, 100% {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
10%, 30%, 50%, 70%, 90% {
-webkit-transform: translate3d(-10px, 0, 0);
transform: translate3d(-10px, 0, 0);
}
20%, 40%, 60%, 80% {
-webkit-transform: translate3d(10px, 0, 0);
transform: translate3d(10px, 0, 0);
}
}
@keyframes shake {
0%, 100% {
-webkit-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
}
10%, 30%, 50%, 70%, 90% {
-webkit-transform: translate3d(-10px, 0, 0);
transform: translate3d(-10px, 0, 0);
}
20%, 40%, 60%, 80% {
-webkit-transform: translate3d(10px, 0, 0);
transform: translate3d(10px, 0, 0);
}
}
.shake {
-webkit-animation-name: shake;
animation-name: shake;
}
最佳答案
这是因为 removeClass 和 addClass 发生得如此之快以至于 DOM 无法跟上。如果你真的讨厌 setTimeout,你可以这样做:
$("#passwordConfig").removeClass('shake animated').delay(50).queue(
function (next) {
$(this).addClass('shake animated');
next();
}
);
Toggle 类也很好,但似乎它只能在每次调用 toggle 时起作用。
关于jquery: addClass, removeClass: 动画只工作一次:为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33222725/