2011-05-18 76 views
2

這裏添加停頓代碼:如何在懸停(jQuery的)

$(document).ready(function(){ 
    $('#testimonials .slide'); 
    setInterval(function(){ 
     $('#testimonials .slide').filter(':visible').fadeOut(1000,function(){ 
      if($(this).next('li.slide').size()){ 
       $(this).next().fadeIn(2000); 
      } 
      else{ 
       $('#testimonials .slide').eq(0).fadeIn(2000); 
      } 
     }); 
    },1000);  
}); 

這適用於UL列表,並想在懸停添加暫停。有什麼建議麼。 ?

+0

你是什麼意思的「暫停」?你能給我們提供一個例子http://jsfiddle.net/ – 2011-05-18 04:46:27

回答

6

您需要保存由setInterval返回的對象,然後您可以將它傳遞給clearInterval以阻止它再次發生。

下面是一些示例代碼,應該讓你像你所追求的:

$(document).ready(function(){ 
    var slider = function() { 
     $('#testimonials .slide').filter(':visible').fadeOut(1000,function(){ 
      if($(this).next('li.slide').size()){ 
       $(this).next().fadeIn(2000); 
      } 
      else{ 
       $('#testimonials .slide').eq(0).fadeIn(2000); 
      } 
     }); 
    }; 

    // save an object so that I can start and stop this as we go 
    var interval = setInterval(slider, 1000); 

    // when the user hovers in, clear the interval; if they hover out, 
    // restart it again 
    $('#testimonials .slide').hover(function() { 
     clearInterval(interval); 
    }, function() { 
     interval = setInterval(slider, 1000); 
    }); 
}); 

請注意,這不是很清楚,我什麼正是用戶將鼠標懸停獲得間隔停止,所以我以爲你想要$('#testimonials .slide')這樣做。如果不是,只需將該部分改爲任何你需要的。

+0

完美的歡呼聲盧克 – 422 2011-05-18 05:09:01

+0

我一直在尋找這一段時間了。我正在尋找錯誤的東西。我很高興我找到了這個! – 2012-11-30 21:06:22

+0

你能幫助這個:http://stackoverflow.com/questions/39598240/how-to-modify-script-to-make-carousel-continuous-and-pause-on-hover – SearchForKnowledge 2016-09-20 15:34:38