动画循环的基本语法?

发布于 2024-08-24 22:19:02 字数 388 浏览 5 评论 0原文

例如,我知道 jQuery 可以制作各种动画。我还知道,在动画的核心,必须有某种循环来执行动画。这种循环的例子是什么?

理想情况下,完整的答案应该回答以下问题:

  • 可以一次为特定对象的单个属性设置动画的有效动画递归的基本语法是什么?该函数应该能够改变其目标对象和对象的属性。
  • 它应该采用什么参数/参数?
  • 重复循环的最佳范围是多少?以毫秒为单位? (这应该是函数的参数/参数吗?)

请记住:

  • 答案不一定是特定于语言的,但如果您使用特定语言编写,请指定是哪种语言。
  • 错误处理是一个优点。 {没有什么比动画做一些奇怪的事情(比如中途停止)更令人恼火的了(就我们的目的而言)。}

谢谢!

I know that jQuery, for example, can do animation of sorts. I also know that at the very core of the animation, there must me some sort of loop doing the animation. What is an example of such a loop?

A complete answer should ideally answer the following questions:

  • What is a basic syntax for an effective animation recursion that can animate a single property of a particular object at a time? The function should be able to vary its target object and property of the object.
  • What arguments/parameters should it take?
  • What is a good range of reiterating the loop? In milliseconds? (Should this be a parameter/argument to the function?)

REMEMBER:

  • The answer is NOT necessarily language specific, but if you are writing in a specific language, please specify which one.
  • Error handling is a plus. {Nothing is more irritating (for our purposes) than an animation that does something strange, like stopping halfway through.}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻了热茶 2024-08-31 22:19:02

通常(至少对于 jQuery)这不是在循环中完成的,而是在一系列回调中完成的。

伪javascript:

function startAnimation(element, endPosition, duration) {
    var startPosition = element.position;
    var startTime = getCurrentTime();
    function animate() {
        var timeElapsed = getCurrentTime() - startTime;
        if (timeElapsed > duration) {
            element.position = endPosition;
            stopTimer();
        } else {
            // interpolate based on time
            element.position = startPosition +
                (endPosition - startPosition) * timeElapsed / duration;
        }
    }
    startRepeatingTimerWithCallbackAndInterval(animate, 1.0 / 30.0);
}

也可以使用对象来存储起始数据而不是闭包。

这并没有完全回答问题中的所有要点,但它是一个起点。

typically (for jQuery at least) this is not done in a loop, but rather in a series of callbacks.

pseudojavascript:

function startAnimation(element, endPosition, duration) {
    var startPosition = element.position;
    var startTime = getCurrentTime();
    function animate() {
        var timeElapsed = getCurrentTime() - startTime;
        if (timeElapsed > duration) {
            element.position = endPosition;
            stopTimer();
        } else {
            // interpolate based on time
            element.position = startPosition +
                (endPosition - startPosition) * timeElapsed / duration;
        }
    }
    startRepeatingTimerWithCallbackAndInterval(animate, 1.0 / 30.0);
}

It's also possible to use objects to store starting data instead of closures.

This doesn't completely answer all the points in the question, but it's a starting point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文