jQuery的循环问题,完成任务的下一个循环

问题描述:

之前,我运行一个简单的循环:jQuery的循环问题,完成任务的下一个循环

var c=0; 

while(c<count){ 
    theimg = $container.find('.img'+c+''); 

    $thisX = theimg.css('top'); 

    theimg.css({ 
     'top':'500px', 
     'display':'block', 
     'opacity':'0' 
    }); 

    theimg.animate({ 
     'opacity':'1', 
     'top':$thisX 
    },800,'linear',function(){ 
     c++; 
    }); 
} 

我已经试过这是一段时间,并都崩溃浏览器。这使我认为它正在创造一个无限循环。

我想循环运行每个元素,一旦动画完成,继续下一步。任何帮助将是巨大的:)

试试这个:

var c=0; 

function nexStep(){ 
    if (c>=count) { 
    return; 
    } 
    var theimg = $container.find('.img'+c+''); 
    var $thisX = theimg.css('top'); 
    theimg.css({ 
     'top':'500px', 
     'display':'block', 
     'opacity':'0' 
    }); 

    theimg.animate({ 
     'opacity':'1', 
     'top':$thisX 
    },800,'linear',function(){ 
     c++; 
     nexStep(); 
    }); 
} 
nexStep(); 
+0

+1 ......我刚开始用这个来的jsfiddle ... – JKirchartz 2012-04-06 12:44:01

+0

感谢,希望我问了大约一个小时前。这工作得很好。 – 2012-04-06 12:45:40