2014-02-19 89 views
0

我有一个滑块,这个作品非常好为止。但现在我仍然需要一个自动播放功能,而且还不知道如何实现这一点。有人有一个想法?自动播放的滑块

var current = 1; 
var wDoc = $(document).width(); 
var box = $(".bg-box"); 
var boxLength = box.length + 2; 
var clipUl = $('.clip ul'); 
var speedAnimation = 1000; 
function slideBox(to) { 
    if (to < 0) { 
     clipUl.css('left', -((boxLength - 2) * wDoc)); 
     to = boxLength - 3; 
    } else if (to > (boxLength - 1)) { 
     clipUl.css('left', -(1 * wDoc)); 
     to = 2; 
    } 
    clipUl.stop().animate({ 
     left: -(to * wDoc) 
    }, speedAnimation); 
    current = to; 
    currentNum = current; 
    $('.pagging-box').removeClass('active'); 
    if (to > (boxLength - 2)) { 
     currentNum = 1; 
    } 
    if (to == 0) { 
     currentNum = boxLength - 2; 
    } 
    $('#pagg' + currentNum).addClass('active'); 

}; 
+0

你想每隔5秒滑动例如? –

+0

尝试'setInterval(function(){//下一张幻灯片的代码},timeToWait)'。定义timeToWait =以毫秒为单位的时间作为显示时间。 –

回答

0

试试这个:

var startAutoPlay = function() { 
    var time = 2000; // (2 seconds); 
    current = 0; 
    setInterval(function(){ 
     slideBox(current); 
     current += 1; 
    }, time); 
} 

startAutoPlay();