简体   繁体   English

如何使用JavaScript对象编写CSS3动画?

[英]How to write CSS3 animations with JavaScript objects?

For the animation library I'm making, I want transitions and animations. 对于我正在制作的动画库,我需要过渡和动画。

Transitions were simple. 过渡很简单。 I can easily access the transition property from the style property of an element object: 我可以轻松地从元素对象的style属性访问transition属性:

   document.body.style['-webkit-transition'] = "background 1s";
   document.body.background = "#f00";

I know that in CSS, there are 2 parts to animations, @-keyframes and then calling the actual animation. 我知道在CSS中,动画有两个部分,@ -keyframes然后调用实际的动画。

How do I assign an @-keyframe rule through pure JavaScript? 如何通过纯JavaScript 分配 @ -keyframe规则?

It wouldn't be something like transitions, because @-keyframe rules aren't applied directly to elements. 不会像过渡,因为@ -keyframe规则不会直接应用于元素。

An alternative to this would be to dynamically create an @-keyframes rule through JavaScript from a string, and append it to a temporary stylesheet. 替代方法是通过JavaScript从字符串动态创建@ -keyframes规则,并将其附加到临时样式表中。 Kind of sloppy, which is why I'm wondering how to do it directly through the DOM. 有点草率,这就是为什么我想知道如何直接通过DOM做到这一点。

Is there a way? 有办法吗? From what I have seen on some other sites, you can play animations and stop them at certain keyframes, but how do you create the animations themselves? 从其他网站上可以看到,您可以播放动画并在某些关键帧处停止它们,但是如何自己创建动画呢?

You have to use the CSSOM, which allows you (among the other things) to manipulate the stylesheets loaded in a document. 您必须使用CSSOM,CSSOM允许您(除其他外)操纵文档中加载的样式表。 First of all, you need a stylesheet loaded in your document, where you'll add the @keyframe rules. 首先,您需要在文档中加载样式表,并在其中添加@keyframe规则。 If you haven't one, you can dynamically create a style HTML element and append it to the head . 如果您还没有,可以动态创建style HTML元素并将其附加到head Then you have to obtain the instance of the CSSStyleSheet interface that represent the stylesheet, you can either retrieve the sheet property of the style (or link ) HTML element, or take it from the list available at document.styleSheets . 然后,您必须获取表示样式表的CSSStyleSheet接口的实例,可以检索style (或link )HTML元素的sheet属性,也可以从document.styleSheets可用列表中获取该属性。

Once you have the object you can simply call the method insertRule on it, passing the whole rule as a string and for second parameter a number indicating the position (before which rule will be added this one). 一旦有了对象,您就可以简单地在其上调用方法insertRule ,将整个规则作为字符串传递,对于第二个参数,通过一个数字指示位置(在此规则之前添加规则)。

If you have to manipulate further the animations, give a look at the APIs: http://www.w3.org/TR/css3-animations/#dom-interfaces http://www.w3.org/TR/DOM-Level-2-Style/css.html 如果您必须进一步处理动画,请看一下API: http : //www.w3.org/TR/css3-animations/#dom-interfaces http://www.w3.org/TR/DOM- Level-2的样式/ css.html

I haven't seen any javascript api solution but it is possible with css styles generated from javaScript. 我还没有看到任何javascript api解决方案,但是可以使用从javaScript生成的css样式。

Check out http://krazyjakee.github.io/jQuery.Keyframes/ if you using jQuery. 如果使用jQuery,请查看http://krazyjakee.github.io/jQuery.Keyframes/

If you whant pure javascript solition then take a look at this post 如果您想使用纯JavaScript隔离,请看看这篇文章

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

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