我试图通过单击“旋转”标志来使盒子旋转 360°。我使用了 CSS3 关键帧动画并将 CSS 应用于 jQuery 函数,但该标志仅触发动画一次;第二次点击时不再旋转。
HTML:
<div id="box"></div>
<a class="activate">spin</a>
jQuery:
$(document).ready(function(){
$(".activate").click(function() {
$('#box').css({
"-webkit-animation-name":"cycle",
"-webkit-animation-duration":"2s",
"-webkit-animation-iteration-count":"1",
"-webkit-animation-fill-mode" : "forwards",
"animation-name":"cycle",
"animation-duration":"2s",
"animation-iteration-count":"1",
"animation-fill-mode" : "forwards",
"-moz-animation-name":"cycle",
"-moz-animation-duration":"2s",
"-moz-animation-iteration-count":"1",
"-moz-animation-fill-mode" : "forwards",
});
});
});
CSS:
#box {
width:100px;
height:100px;
background-color:black;
margin-bottom:10px;
}
@-webkit-keyframes cycle {
from {-webkit-transform: rotate(0deg);}
to {-webkit-transform: rotate(360deg);}
}
@keyframes cycle {
from {transform: rotate(0deg);}
to {transform: rotate(360deg);}
}
@-moz-keyframes cycle {
from {-moz-transform: rotate(0deg);}
to {-moz-transform: rotate(360deg);}
}
.activate {
margin-top:5px;
padding:3px 5px;
background-color:#333;
color:#eee;
cursor:pointer;
}
我查遍了 StackOverflow,但一无所获;我必须使用 addClass 并触发+重置动画吗?
任何帮助将不胜感激!
最佳答案
这是实现这一目标的最佳方法。
为动画制作事件类
#box.active {
-webkit-animation-name: cycle;
-webkit-animation-duration: 2s;
-webkit-animation-iteration-count: 1;
-webkit-animation-fill-mode: forwards;
animation-name: cycle;
animation-duration: 2s;
animation-iteration-count: 1;
animation-fill-mode: forwards;
-moz-animation-name: cycle;
-moz-animation-duration: 2s;
-moz-animation-iteration-count: 1;
-moz-animation-fill-mode: forwards;
}
并在每次动画结束后使用 JQuery 删除事件类
$(document).ready(function() {
$(".activate").click(function() {
$('#box').addClass('active');
$('#box').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend',function(){
$(this).removeClass('active');
});
});
});
关于jquery - 单击时刷新 CSS 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45047218/