如何停止幻灯片循环播放

时间:2017-01-15 21:57:28

标签: javascript loops slideshow

我有一个简单的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>

希望有人可以提供帮助。

1 个答案:

答案 0 :(得分: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);
});
相关问题