2013-04-11 116 views
0

我该如何做到这一点更简单?jquery动画回调开始next动画

var i = 0; 
    $(this).find(".ui-stars-star-on-large:eq("+i+")").animate({width: w+'px'}, 200, "swing", function() { 
     i++; 
     $(this).parent().find(".ui-stars-star-on-large:eq("+i+")").animate({width: w+'px'}, 200, "swing", function() { 
      i++; 
      $(this).parent().find(".ui-stars-star-on-large:eq("+i+")").animate({width: w+'px'}, 200, "swing", function() { 
       i++; 
       $(this).parent().find(".ui-stars-star-on-large:eq("+i+")").animate({width: w+'px'}, 200, "swing", function() { 
        i++; 
        $(this).parent().find(".ui-stars-star-on-large:eq("+i+")").animate({width: w+'px'}, 200, "swing", function() { 
         i++; 
        }); 
       }); 
      }); 
     }); 
    }); 
+1

你需要证明你的标记。可能最好为此创建一个小提琴来展示你的工作。 – 2013-04-11 21:14:00

+0

Humm,我会尝试并返回。 – pkdkk 2013-04-11 21:15:43

+0

我不认为标记是必要的。看起来递归可以在这里帮助。 – 2013-04-11 21:17:50

回答

2

假设要动画都下父母,你能说出你的动画回调的事情。 ..然后你可以从内部再次触发动画。

var $stars = $(this).children('.ui-stars-star-on-large'); 
var i = 0; 

// wrap this whole block in a named function expression, so it can be re-called 
(function nextStar() { 
    // if you want the animations to loop, remove the if 
    // and add a `i %= $stars.length;` after the jquery stuff 
    if (i < $stars.length) { 
     // .eq(i) works much like the ':eq('+i+')' selector, except 
     // it's less hideous and doesn't require building selectors. :P 
     $stars.eq(i++).animate({width: w+'px'}, 200, 'swing', nextStar); 
    } 
// end the function and immediately call it, starting the first animation 
})(); 
1

你可以使用递归:

var w = 200;  
function animate(count, i) { 
    $(this).find(".ui-stars-star-on-large:eq(" + i + ")").animate({ 
     width: w + "px" 
    }, 200, "swing", function() { 
     i++; 
     if (i < count) animate(count, i); 
    }} 
} 

animate(5, 1); 

,或者使用延迟:

for(var i = 0; i < 5; i++) { 
    $(this).find(".ui-stars-star-on-large:eq(" + i + ")") 
     .delay(i * 200) 
     .animate({ 
      width: w + "px" 
     }, 200, "swing") 
} 
1

你可以只使用周期,如果setTimeout动画轻,有不错的表现

var $this = $(this), 
    $stars = $this.parent().find(".ui-stars-star-on-large"); 
for (var i = 0; i < 5; i++) { 
    var _i = i; 
    setTimeout(function(){ 
     $stars().eq(_i).animate({width: w+'px'}, 200, "swing") 
    }, 200*i); 
}