2012-11-22 125 views
2

我得到了一堆我已经设法逐渐淡入的块状引用。此时,在最后一个淡入淡出之后,该功能结束。但我希望它能够循环并从头开始。这是我的代码到目前为止的作品:jQuery循环。每个函数

$("div.quotes blockquote").each(function (index){ 
    $(this).delay(4500*index).fadeIn(500).delay(4000).fadeOut(500); 
}); 

如何让它循环?

回答

2

一个可能的解决方案:

function run() { 
    var els = $("div.quotes blockquote"); 
    els.each(function(i) { 
     $(this).delay(4500 * i).fadeIn(500).delay(4000).fadeOut(500, function() { 
      if (i == els.length - 1) run(); 
     }); 
    }); 
} 
run(); 

DEMO:http://jsfiddle.net/eDu6W/

+1

感谢所有答案但这是唯一的答案 我用了。这是最简单的解决方案之一,并做我需要的 – Coop

0
function toggleBlockQuotes() 
{ 
    var countBlockquotes = $("div.quotes blockquote").length; 
    var b = 1; 
    $("div.quotes blockquote").each(function (index) 
    { 
     $(this).delay(4500*index).fadeIn(500).delay(4000).fadeOut(500); 
     b++; 
     if(b == countBlockquotes) 
     { 
      toggleBlockQuotes(); 
     } 
    }); 
} 

请注意,这将创建一个无限循环。

0
function loop() { 
    $("div.quotes blockquote").each(function(index) { 
     $(this).delay(4500 * index).fadeIn(500).delay(4000).fadeOut(500, function() { 
      loop(); 
     }); 
    }); 
} 

loop();​ 
0

可以做到这样的,要看你需要的究竟是什么: http://jsfiddle.net/MmPgG/

(function loopAnim() { 
    var $elems = $("div") 
    $elems.each(function(index) { 
     var $that = $(this); 
     (function() { 
      $that.delay(4500 * index).fadeIn(500).delay(4000).fadeOut(500, function() { 
       if (index == $elems.length - 1) { 
        $elems.show(); 
        loopAnim() 
       } 
      }); 

     })($that) 
    }); 
})()​