2011-11-07 25 views
0

我有一點javascript/jQuery創建一個簡單的幻燈片。我正在學習jquery,因爲我去了這裏......並且我很難理解如何在停止之前修改這個JS循環x次。現在,它在無限循環上播放幻燈片。有什麼建議麼?謝謝!簡單的幻燈片jQuery,需要它5張幻燈片後結束。幫幫我?

<script> 
function slideSwitch() { 
    var $active = $('#slideshow DIV.active'); 

if ($active.length == 0) $active = $('#slideshow DIV.item:last'); 

var $next = $active.next().length ? $active.next() 
    : $('#slideshow DIV.item:first'); 

$active.addClass('last-active'); 

$next.css({opacity: 0.0}) 
    .addClass('active') 
    .animate({opacity: 1.0}, 1000, function() { 
     $active.removeClass('active last-active'); 
     }); 
} 

    $(function() { 
     setInterval("slideSwitch()", 5000); 
    }); 
</script> 

回答

1

只是clearTimeout()後的5倍:

var timeout, count = 0, x = 5; // change 5 to the amount of times you want it to go 

function slideSwitch() 
{ 

    var $active = $('#slideshow DIV.active'); 

    if ($active.length == 0) $active = $('#slideshow DIV.item:last'); 

    var $next = $active.next().length ? $active.next() 
    : $('#slideshow DIV.item:first'); 

    $active.addClass('last-active'); 

    $next.css({opacity: 0.0}) 
    .addClass('active') 
    .animate({opacity: 1.0}, 1000, function() { 
     $active.removeClass('active last-active'); 
    }); 

    count++; 
    if(count == x) clearTimeout(timeout); 

} 

$(function() { 
    timeout = setInterval(slideSwitch, 5000); 
}); 
+0

完美,這工作完美。非常感謝你! –

1
<script> 

var loop = 0; 

function slideSwitch() { 

    var $active = $('#slideshow DIV.active'); 

if ($active.length == 0) $active = $('#slideshow DIV.item:last'); 

var $next = $active.next().length ? $active.next() 
    : $('#slideshow DIV.item:first'); 

$active.addClass('last-active'); 

$next.css({opacity: 0.0}) 
    .addClass('active') 
    .animate({opacity: 1.0}, 1000, function() { 
     $active.removeClass('active last-active'); 
     }); 
} 
    loop++; 


    $(function() { 
     if(loop < 5){ 
      setInterval("slideSwitch()", 5000); 
     } 
    }); 
</script> 
+0

我想這個版本,但它仍然在無限循環。我有一個工作版本由其他人張貼..但感謝您的時間:) –