简体   繁体   English

用Javascript控制CSS动画

[英]Controlling a CSS animation with Javascript

I have a CSS animation that I would like to control with JS 我有一个想用JS控制的CSS动画

My animation: 我的动画:

http://jsfiddle.net/link2twenty/ny5rb41f/ http://jsfiddle.net/link2twenty/ny5rb41f/

@keyframes dash {
    from {
        stroke-dashoffset: 55;
    }
    to {
        stroke-dashoffset: 0;
        stroke-width: 2;
    }
}

@keyframes spinner {
    from {
        transform: rotate(-315deg);
        filter: grayscale(100%);
        -webkit-filter: grayscale(100%);
        -moz-filter: grayscale(100%);
        -ms-filter: grayscale(100%); 
        -o-filter: grayscale(100%);
        opacity: .2;
    }
    to {
        transform: rotate(0deg);
         filter: grayscale(0%);
        -webkit-filter: grayscale(0%);
        -moz-filter: grayscale(0%);
        -ms-filter: grayscale(0%); 
        -o-filter: grayscale(0%);
        opacity: 1;
    }
}

In my application , currently when a user pulls down on the screen a classes style is updated. 我的应用程序中 ,当前当用户在屏幕上下拉菜单时,将更新类样式。 This sometimes causes lag, and with my new animation being more complex I fear it may cause even more lag. 有时这会导致延迟,而随着我的新动画更加复杂,我担心它可能会导致更大的延迟。

I was thinking maybe have a few classes at different stages and then update the elements class as it gets to the right distance, but then to get it smooth we need really long code, 我当时想也许在不同的阶段有几个类,然后在元素类到达正确的距离时对其进行更新,但是要使其平滑,我们需要很长的代码,

So the real question is; 因此,真正的问题是; is there a way to control css animations with JavaScript without having to write each part of the animation as a separate class. 有没有一种方法可以用JavaScript控制CSS动画,而不必将动画的每个部分编写为单独的类。

You could create a class call animate and in this class you put all the animate properties. 您可以创建一个称为animate的类,并在该类中放置所有animate属性。 Later with javascript, in this case jQuery, you could add this class to start the animation like this: 稍后使用javascript(在本例中为jQuery)中,您可以添加此类以启动动画,如下所示:

 $('button').click(function () { $(".refreshIcon").addClass("animate"); $(".refreshStem").addClass("animate2"); }); 
 .container { background: white; } .animate { animation: spinner 3s; animation-fill-mode:forwards; animation-iteration-count: 1; } .animate2 { animation: dash 3s linear; animation-fill-mode:forwards; animation-iteration-count: 1; } .refreshIcon { pointer-events: none; display: block; height: 32px; width: 32px; } .refreshStem { stroke-dasharray: 55; fill: rgba(0, 0, 0, 0); stroke: blue; stroke-width: 1; } .refreshHead { fill: blue; stroke-width: 0; } @keyframes dash { from { stroke-dashoffset: 55; } to { stroke-dashoffset: 0; stroke-width: 2; } } @keyframes spinner { from { transform: rotate(-315deg); filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); -o-filter: grayscale(100%); opacity: .2; } to { transform: rotate(0deg); filter: grayscale(0%); -webkit-filter: grayscale(0%); -moz-filter: grayscale(0%); -ms-filter: grayscale(0%); -o-filter: grayscale(0%); opacity: 1; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <svg class="refreshIcon" viewBox="0 0 24 24"> <g> <path class="refreshStem" transform="rotate(-45 12.000000476837158,12.000000476837158), translate(24), scale(-1, 1)" d="m5.02361,12c0,-3.87126 3.12203,-7.00698 6.97639, -7.00698c3.85436,0 6.97639,3.13572 6.97639, 7.00698c0,3.87126 -3.12203,7.00698 -6.97639,7.00698c-3.85436, 0 -6.97639,-3.13572 -6.97639,-7.00698z" /> <rect fill="#ffffff" stroke-width="0" x="17" y="11.029" width="4" height="3" stroke="#ffffff"/> <path class="refreshHead" d="m12.97192,10.96708l0,-6.99941l7.06761,6.99941l-7.06761,0z" transform="rotate(-90 16.5,7.5) " /> </g> </svg> <button >start</button> 

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

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