【问题标题】:wait for each jQuery等待每个 jQuery
【发布时间】:2010-01-30 16:08:32
【问题描述】:

我正在尝试使每个语句中的 div 淡入/淡出。问题是在淡入/淡出完成之前调用了下一个项目。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script>

<div id='one'>one</div>
<div id='two'>two</div>
<div id='three'>three</div>

<script>
$.each([ "one", "two", "three"], function() {
  console.log( 'start - ' + this );
  animate( this );
  console.log( 'end - ' + this );
});

function animate( id )
{
  box = '#' + id;

  $(box).fadeOut( 500, function( )
  {

    console.log('showing - ' + id);
    $(box).fadeIn( 500 );
    $(box).css('backgroundColor','white');

  });

}
</script>

控制台显示 -

start - one
end - one
start - two
end - two
start - three
end - three
showing - one
showing - two
showing - three

我想要类似的东西 -

start - one
showing - one
end - one
start - two
showing - two
end - two
start - three
showing - three
end - three

那么我如何才能等待每个“每个”都完全完成,然后再转到下一个值?

【问题讨论】:

    标签: jquery wait each


    【解决方案1】:

    您将不得不使用回调——在当前函数完成时执行的函数。要使用 .fadeOut 执行此操作,您可以:

    $('#element').fadeOut( 400, myFunction );
    

    在fadeOut 完成之前不会调用myFunction。使用 $.get 的 AJAX 调用也可以有回调函数。

    这是一个可行的示例,尽管我确信有更好的方法:

    function animate(myArray, start_index) {
    
        // Stealing this line from Sam, who posted below.
        if(!start_index) start_index = 0;
    
        next_index = start_index+1;
        if(next_index > myArray.length) { return; }
    
        box = '#' + myArray[start_index]; 
        $(box).fadeOut(500, function() { animate(myArray,next_index); });
    }
    

    然后在您的 document.ready 中调用:

    animate(theArray);
    

    【讨论】:

      【解决方案2】:

      听起来您正试图“循环”通过 div 列表。你检查过jQuery Cycle plugin吗?

      【讨论】:

      • 好吧,这只是一个简单的例子。我可能想对数组中的值进行 ajax 调用,调用其他执行其他操作的函数或其他操作。我做了以下内容来展示一个简单的示例,说明如何让每个项目等待前一个项目完全结束。
      【解决方案3】:

      怎么样,通过遍历函数内数组中的每个项目来制作动画?

      var elements = [ "one", "two", "three"];
      animate(elements);
      
      function animate( elements, index )
      {
          if(!index) index = 0;
          var box = '#' + elements[index];
          var $$box = $("#box");
          console.log( 'start - ' + elements[index] );
          $$box.fadeOut( 500, function( )
          {
              console.log('showing - ' + elements[index]);
              $$box.fadeIn( 500, function() {
                  console.log( 'end - ' + elements[index] );
                  if(elements[++index]) animate(elements, index);
              } ).css('backgroundColor','white');
          });
      }
      

      如果你愿意,你甚至可以循环回到起点:

      var elements = [ "one", "two", "three"];
      animate(elements);
      
      function animate( elements, index )
      {
          if(!index) index = 0;
          var box = '#' + elements[index];
          var $$box = $(box);
          console.log( 'start - ' + elements[index] );
          $$box.fadeOut( 500, function( )
          {
              console.log('showing - ' + elements[index]);
              $$box.fadeIn( 500, function() {
                  $$box.css('backgroundColor','white');
                  console.log( 'end - ' + elements[index] );
                  // go to next element, or first element if at end
                  index = ++index % (elements.length);
                  animate(elements, index);
              } );
          }).css('backgroundColor', 'aqua');
      }
      

      【讨论】:

      • 这是对我发布的确切示例的更完整答案;虽然在我的辩护中,我试图让它成为一个简单的递归示例,就像我在这里的评论:stackoverflow.com/questions/2168485/wait-for-each-jquery
      • 你的例子比较简单,有时可能会有所帮助(更容易理解,取决于你想做什么)
      猜你喜欢
      • 2015-07-17
      • 2013-10-19
      • 2018-11-08
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多