这是我的 jquery 图像滑块的代码。它具有下一个和上一个按钮,并且在悬停时会暂停,但是当它到达最后一张幻灯片时,它会开始一遍又一遍地循环第一张幻灯片。任何帮助,将不胜感激。我似乎无法弄清楚,我盯着它看我的大脑很痛..
$(document).ready(function(){
window.currentIndex = 0;
function slide(index) {
index = index >= $('#slides img').length ? 0 : index;
$('#caption, #slide').fadeOut(500, function () {
$el = $('#slides img:eq('+index+')');
$('#caption').text($el.attr('alt'));
$('#slide').attr('src', $el.attr('src'));
$('#caption, #slide').fadeIn(500);
});
}
$("#featuredSlider").mouseenter(function(){
if (timer) {
clearInterval(timer);
}
});
$("#featuredSlider").mouseleave(function(){
timer = setInterval(function(){
slide(++window.currentIndex);
},3000);
}).mouseleave();
//bind next / prev buttons
$("#featuredPrev").on('click', function () {
slide(--window.currentIndex);
});
$("#featuredNext").on('click', function () {
slide(++window.currentIndex);
});
});
这是HTML:
<section id="featuredSlider">
<img id="slide" src="images/image1.png" alt="Hello">
<div id="slides">
<img src="images/image1.png" alt="">
<img src="images/image2.png" alt="">
<img src="images/image3.png" alt="">
</div>
<div id="arrows">
<div id="featuredPrev"></div>
<div id="featuredNext"></div>
</div>
</section>