2013-03-14 151 views
4
$('.ip_button').click(function(){ 
    var buttonValue, slideTimes; 
    buttonValue = $(this).html(); 
    slideTimes = parseInt(buttonValue) - 1; 
    for (var i = 0 ; i < slideTimes; i++) { 
      slider.gallery_next(); 
     }    
}); 

您好我想滑动多个图像滑块使用函数,确定按钮-1的值和运行幻灯片多次。不幸的是,无论按钮中有什么值,函数都只能执行一次。当前按钮中的值为:循环迭代函数x次循环

<button class="ip_button">5</button> 

gallery_next()函数;封装和是这样的:

this.gallery_next = function() { 
    if (galleryRotating) { return; } 
    galleryRotating = true; 
    var elem = $(galleryId + " > li").first(); 
    $(galleryId).append(elem.clone()); 
    $(galleryId).animate({"left" : -imgWidth}, 650, function() { 
     elem.remove(); 
     galleryRotating = false; 
     $(galleryId).css({"left" : 0}); 
    }); 

回答

3

animate不会因时间for循环再次调用函数完成,所以galleryRotation仍然是真实的,并且功能会做什么。你需要一个不同的方法:设置一个计时器来调用你的函数,比如700ms或者(更好)在你传递给animate的回调中再次调用旋转。

+0

请检查: http://jsfiddle.net/inser/LwhhD/3/ – inser 2013-03-14 13:33:04

+0

它实际上是放入队列:http://api.jquery.com/animate/ 在你的问题你做检查动画效果是否已经激活,因此动画未被排入队列。在jsfiddle示例中,您不检查它,所以动画排队。 – Aneri 2013-03-14 13:40:54