到达最后一张幻灯片后返回第一张幻灯片 - 使用jQuery Slider

时间:2013-01-10 07:28:12

标签: jquery slider

我有一个带自动高级功能的jQuery滑块。但我想知道如何在到达最后一张幻灯片后返回到第一张幻灯片。 currentSlide变量(保存我们正在查看的当前幻灯片的索引)只是越来越大,在达到最后一张幻灯片后没有返回到零。我没有为滑块本身包含我的所有jQuery脚本,因为这将是巨大的,我认为没有必要。这是代码:

<html>
<head>
</head>

<body>
<div class="slider" data-slide="0">
    <div class="slide"></div>
    <div class="slide"></div>
    <div class="slide"></div>
    <div class="slide"></div>
</div>

<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">

$(window).load(function(){
    var currentSlide = $(".slider").attr("data-slide"); /*Every time a slide is viewed, the index number of that slide is added to an html5 data- attribute. That way, we know what slide we are on. This var stores that information. */
    var timeOut = null;

    (function autoAdvance(){
        $('.sliderNav').find('.sliderTrigger' + (currentSlide)).trigger('click',[true]); /* Automatically click the trigger that will advance the slider to the next slide. */
        currentSlide++;
        timeOut = setTimeout(autoAdvance,5000);
    })();

});

</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

...
var currentSlide = $(".slider").attr("data-slide"); 
var timeOut = null;
count = $(".slider").children().length;

(function autoAdvance(){
    $('.sliderNav').find('.sliderTrigger' + (currentSlide)).trigger('click',[true]); 
    if (count == currentSlide){
        currentSlide = 1;
    } else {
        currentSlide++;
    }
    timeOut = setTimeout(autoAdvance,5000);
    })();
...