简体   繁体   English

使用纯 javascript 创建动画或效果的正确方法是什么?

[英]What is the correct way to create animations or effects with pure javascript?

I want to know is it the correct way to use setTimeOut or setInterval for making an animation in javaScript (I mean the loop part of the animation, looping after specific time) ?我想知道使用setTimeOutsetInterval在 javaScript 中制作动画的正确方法(我的意思是动画的循环部分,在特定时间后循环)?

if it is the correct and the only way of doing that , do the green sock (Gsap) and the other JS animation library use the setTimeOut and setInterval for doing the animation in specific time ?如果这是正确且唯一的方法,那么绿色袜子(Gsap)和其他 JS 动画库是否使用setTimeOutsetInterval在特定时间执行动画? what about the animate method in jQuery ? jQuery 中的animate方法怎么样?

UPDATE更新

I'm using the GreenSock Animation Platform (Gsap) for animations , and it provides really fast and smooth animations.我正在使用GreenSock 动画平台 (Gsap)制作动画,它提供了非常快速和流畅的动画。 I just asked the question to figure out how do they handle the animations very smooth and fast?我只是问这个问题来弄清楚他们如何非常流畅和快速地处理动画?

as I mentioned in the question , there is some functions like setTimeOut and setInterval for handling animations and also there is another function name is requestAnimationFrame() for making animations without gaps unlike the timeOut and Interval functions.正如我在问题中提到的,有一些函数,如setTimeOutsetInterval用于处理动画,还有另一个函数名称是requestAnimationFrame()用于制作与 timeOut 和 Interval 函数不同的无间隙动画。

So with these definitions how does the fast animation libraries like green sock and velocity.js handles their animations?那么有了这些定义,像green sock 和velocity.js 这样的快速动画库是如何处理它们的动画的呢?

Thank you .谢谢你 。

I created a mobile that that has some animations for a Light and Sound Mind Machine.我创建了一个移动设备,它为 Light and Sound Mind Machine 提供了一些动画。

I used setTimeout and it did not perform well.我使用了 setTimeout 并且它表现不佳。 There is a lag that you have to handle to make it smooth.您必须处理一个滞后才能使其顺利进行。

Instead I used requestAnimationFrame() and I handled the FPS (frames per second) in my JavaScript and performed the animation.相反,我使用 requestAnimationFrame() 并在 JavaScript 中处理 FPS(每秒帧数)并执行动画。 You can count on requestAnimationFrame to give you the performance you want, where settimeout or interval will have some gaps.您可以依靠 requestAnimationFrame 为您提供所需的性能,其中 settimeout 或 interval 会有一些差距。

https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame

Don't bother with jQuery for animations (unless it is something very simple), it is slow, and on Android it is unusable.不要为动画而烦恼 jQuery(除非它非常简单),它很慢,而且在 Android 上它是不可用的。

Instead... use Velocity.js to handle some other annimations.相反……使用Velocity.js来处理其他一些动画。 It is smooth as silk.它像丝绸一样光滑。

Velocity is an animation engine with the same API as jQuery's $.animate() . Velocity 是一个动画引擎,其 API 与 jQuery 的 $.animate() 相同 It works with and without jQuery .它可以使用和不使用 jQuery It's incredibly fast, and it features color animation, transforms, loops, easings, SVG support, and scrolling.非常快,并且具有彩色动画、变换、循环、缓动、SVG 支持和滚动功能。 It is the best of jQuery and CSS transitions combined.它是结合 jQuery 和 CSS 过渡的最佳选择。

http://velocityjs.org/ http://velocityjs.org/

Use requestAnimationFrame if you HAVE to use JavaScript, however consider if you can achieve your desired animation using CSS3 transitions instead of JavaScript.如果您必须使用 JavaScript,请使用 requestAnimationFrame,但请考虑是否可以使用 CSS3 过渡而不是 JavaScript 来实现您想要的动画。

See https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFramehttps://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame

And https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitionshttps://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions

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

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