Jquery循环问题,在下一个循环之前完成任务

时间:2012-04-06 12:38:41

标签: jquery loops for-loop while-loop

我正在运行一个简单的循环:

    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++;
    });
}

我已经尝试了这个,因为而且两者都崩溃了浏览器。这让我觉得它正在创造一个无限循环。

我希望循环运行每个元素,一旦动画完成,继续下一个。任何帮助都会很棒:)

1 个答案:

答案 0 :(得分:1)

试试这个:

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();
相关问题