jquery - 单击时刷新 CSS 动画

标签 jquery css animation rotation

我试图通过单击“旋转”标志来使盒子旋转 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;
}

Here's my JSFiddle.

我查遍了 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');
        });

    });
});

jsfiddle

关于jquery - 单击时刷新 CSS 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45047218/

相关文章:

javascript - 将新点添加到点数组中的正确位置

javascript - 隐藏推送的属性

javascript - 如何使用多个验证码获取验证码值?

jquery 高度/宽度 if 语句

html - 是什么导致这些 SVG/CSS 动画在某些浏览器中中断?

jquery - 如何获取on()上的:selected form <option> when using .?

html - 缩小浏览器时,CSS组按钮div脱离行和列

css - Bootstrap 3 和 CSS3 - 导航栏搜索框放置错误

android - CardView翻转动画

android - 如何将动画添加到 Activity 完成()