2017-01-15 14 views
0

私は、うまく動作しますが現在ループしているシンプルなjavascriptスライドショーを持っていますが、一度実行してから停止するだけです。私はしばらく頭を傷つけましたが、残念ながら私はコーダーではありませんので、私はあなたに助けに来ます。ここにjavascriptがあります:スライドショーのループを止める方法

<script type="text/javascript"> 

function slideSwitch() { 
var $active = $('#slideshow IMG.active'); 

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

// use this to pull the images in the order they appear in the markup 
var $next = $active.next().length ? $active.next() 
    : $('#slideshow IMG:first'); 

// uncomment the 3 lines below to pull the images in random order 

// var $sibs = $active.siblings(); 
// var rndNum = Math.floor(Math.random() * $sibs.length); 
// var $next = $($sibs[ rndNum ]); 

$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()", 6000); 
}); 

</script> 

誰かが助けることを望みます。

+0

あなたが探しているのは、 'setInterval'ではなく' setTimeout'です。 – char

答えて

0
var interval = null; 
function slideSwitch() { 
    var $active = $('#slideshow IMG.active'); 

    var $next = $active.next(); 
    if($next.length == 0){ 
     // if there is no more images, don't loop but instead stop 
     if(interval) 
      clearInterval(interval); 
     return; 
    } 

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

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

$(function() { 
    // make sure there is an .active image (preferably the first) 
    if($('#slideshow IMG.active').length == 0) 
     $('#slideshow IMG:first').addClass("active"); 

    // store the interval 
    interval = setInterval("slideSwitch()", 6000); 
}); 
+0

このような迅速な対応に感謝します。それは論理的ですが、残念ながら私は物理的にスクリプトを正しく変更する方法は不明です。あなたが私を見せてくれれば、とても感謝しています。 – Mark

+0

ちょっとした間違いを修正しました。 '.addClass(" active ");' not '.addClass(" .active ");'。 –

+0

はい、私はそれに気付きました。ありがとう。 – Mark

関連する問題