简体   繁体   English

使用CSS3或JS的连续旋转动画?

[英]Continuous rotation animation using css3 or js?

I have some objects in an animation which are continously animating in a rotation back and forth using css3. 我在动画中有一些对象,它们使用css3不断地来回旋转动画。 To do this I have created a declaration like so: 为此,我创建了一个声明,如下所示:

@-webkit-keyframes wiggle {
0% {-webkit-transform:rotate(12deg);}
50% {-webkit-transform:rotate(-6deg);}
100% {-webkit-transform:rotate(12deg);}
}

And each object I want to use this for I do the following: 我想要使​​用的每个对象都需要执行以下操作:

.p4, .p5, .p6 {
-webkit-animation-name: wiggle; 
-webkit-animation-iteration-count: infinite; 
-webkit-animation-timing-function: ease-in-out;
}

Since I want some random between each of the objects I have the following altering animation durations: 由于我希望每个对象之间都有一些随机性,因此需要更改动画的持续时间:

.p4 {
-webkit-animation-duration: 5s; 
}

.p5 {
-webkit-animation-duration: 7s; 
}

.p6 {
-webkit-animation-duration: 8s; 
}

And so on... 等等...

This works ok - (only testing in Chrome so far). 这项工作正常-(目前为止仅在Chrome中测试)。 But it doesn't seem to be very optimal. 但这似乎不是最佳的。

I would like to know whether there is a faster way to achieve this. 我想知道是否有更快的方法来实现这一目标。 Or a leaner way. 或更精简的方式。 I believe I could perform this with JS but I'm not sure what is going to be more lightweight on the end users resources. 我相信我可以使用JS执行此操作,但是我不确定最终用户资源上的内容将变得更轻量级。

Is there a better way to achieve this sort of basic animation with less resources - and if so, how? 有没有更好的方法可以用更少的资源来实现这种基本的动画-如果是的话,如何?

In addition to this, if I were to create this same animation using jquery for example, how could I actually test the memory usage required? 除此之外,例如,如果要使用jquery来创建相同的动画,我该如何实际测试所需的内存使用情况? I found something recently to test memory usage of tabs but it appears the results are inconsistent. 我最近发现了一些东西来测试选项卡的内存使用情况,但结果似乎不一致。 ie sometimes 1 tab is using more memory than the other and vice versa even though the code remains the same. 也就是说,即使代码保持不变,有时一个选项卡使用的内存也比其他选项卡多,反之亦然。

Thanks for any pointers. 感谢您的指导。

Stick with the CSS3 animations if they work as intended, and if needed do something in JS as a fallback for non supporting browsers. 如果CSS3动画可以按预期工作,请坚持使用CSS3动画;如果需要,可以在JS中做一些事情,作为不支持浏览器的后备。

Replacing the CSS3 with JS is not really leaner or less resource intensive, quite the opposite as CSS3 seems to have smoother animations and in some browsers will use hardware accelaration and the GPU, something not possible with JS (at least not easily). 用JS替换CSS3并不是真正的精简或资源密集型,相反,因为CSS3似乎具有更流畅的动画,并且在某些浏览器中将使用硬件加速和GPU,这在JS中是不可能的(至少不容易)。

All in all you'll end up writing more in JS, and perhaps use a library or plugin for rotating animations as well. 总而言之,您最终将用JS编写更多内容,并且可能还使用库或插件来旋转动画。 It will be less smooth in most browsers, and use more resources as well as JS will have to set the css tranform values several times each second for a somewhat smooth animation. 在大多数浏览器中,它将变得不太平滑,并且将使用更多的资源,而JS将不得不每秒多次设置css转换值,以使动画更加平滑。

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

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