简体   繁体   English

jQuery-帮助.animate和函数回调

[英]JQuery - Help with .animate and function callback

I am trying the following, but cant get the function to run itself again (I am trying to create some kind of looping animation) 我正在尝试以下操作,但是无法使该函数再次运行(我正在尝试创建某种循环动画)

$(document).ready(function() {



       //function loopingFunction(){

       function loop() {
            $('#item').animate({top:'+=100'}, 500, function() {
                $('#item').animate({top:'-=100'},500, function(){
                    loop;
                });
            });
       }


});

Either: 要么:

function loop() {
        $('#item').animate({top:'+=100'}, 500, function() {
            $('#item').animate({top:'-=100'},500, function(){
                loop();
            });
        });
   }

Live Example: http://jsfiddle.net/vyef6/ 实时示例: http//jsfiddle.net/vyef6/

or 要么

function loop() {
        $('#item').animate({top:'+=100'}, 500, function() {
            $('#item').animate({top:'-=100'},500, loop);
        });
   }

Live example: http://jsfiddle.net/w92b2/ 实时示例: http//jsfiddle.net/w92b2/

Explaination: In the first one, you are literally executing the loop function. 说明:在第一个示例中,您实际上是在执行loop功能。 Therefore needs the parenthesis. 因此需要括号。 Ine the second you're passing a reference,or callback, to the loop function in to animate - therefore it doesnt need the parenthesis. 在第二个步骤中,您将向loop函数传递引用或回调以进行animate -因此它不需要括号。

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

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