2011-11-07 7 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

私はこのバージョンを試してみましたが、それは無限ループにまだありました。私は他の誰かによって投稿された作業版を得ました。しかし、あなたの時間に感謝:) –

関連する問題