我有一个带有自动前进功能的 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>