开发者

JQuery - Help with .animate and function callback

开发者 https://www.devze.com 2023-04-03 06:31 出处:网络
I am trying the following, but cant get the function to run itself again (I am try开发者_高级运维ing to create some kind of looping animation)

I am trying the following, but cant get the function to run itself again (I am try开发者_高级运维ing 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/

or

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

Live example: http://jsfiddle.net/w92b2/

Explaination: In the first one, you are literally executing the loop function. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消