[英]Making a jQuery image slider stop at last image and adding next and prev buttons
I have the following example which goes through each LI and if you hover image will pause and the continue when you mouseout but this is as far as I have got. 我有下面的示例,它遍历每个LI,如果您将鼠标悬停在鼠标悬停时图像会暂停并继续,但是据我所知。
I would like it to stop when it gets to the last LI and also would like to make the 2 links I added wth classes "next" and "prev" so if clicked will move to next or prev image. 我希望它在到达最后一个LI时停止,并且还想使我添加两个类的链接“ next”和“ prev”,因此如果单击该链接将移至下一个或上一个图像。 Of course if at first image to set a class on prev like hidden and if on last image set next button to hidden so cannot be used, however this is as far as I have got.
当然,如果首先在上一个图像上将类设置为隐藏,而在最后一个图像上将下一个按钮设置为隐藏,那么将无法使用,但是据我所知。
$( function() { var timer; lis = $(".productImage"); function showNext() { var active = lis.filter(".active").removeClass("active"); var next = active.next(); if (next.length===0) { next = lis.eq(0); } next.addClass("active"); } function showPrev() { var active = lis.filter(".active").removeClass("active"); var prev = active.prev(); if (prev.length===0) { prev = lis.eq(0); } prev.addClass("active"); } function playGallery() { stopGallery(); timer = window.setInterval(showNext, 2500); } function stopGallery() { if (timer) window.clearTimeout(timer); } // move to next image $('.galleryNext').on('click', function(event){ stopGallery(); showNext(); }); // move to previous image $('.galleryPrev').on('click', function(event){ stopGallery(); showPrev(); }); // reset slider timer if thumbnail changed $('.thumbnail-list li a').on('click', function(event){ stopGallery(); playGallery(); }); // pause image slider if mouse is hovering gallery main image $(".featured-image-list") .on("mouseleave", playGallery) .on("mouseenter", stopGallery); playGallery(); });
.productImage { display : none; } .productImage.active { display: block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul class="featured-image-list"> <li class="productImage active">1</li> <li class="productImage">2</li> <li class="productImage">3</li> <li class="productImage">4</li> <li class="productImage">5</li> </ul> <a class="galleryPrev">prev</a> <a class="galleryNext">next</a>
modifying your code a bare minimum I've come up with this: 修改您的代码几乎没有,我想出了这个:
Simply placing showNext() and showPrev() within the functions below gets the next and prev buttons working with the caveat that it also stops the slides from auto progressing (and I see in your edit that's what you did): 只需将showNext()和showPrev()放在下面的函数中,即可获得下一个和上一个按钮,但需要注意的是,这也会阻止幻灯片自动前进(我在编辑中看到的是您所做的):
function nextImage() {
stopGallery();
showNext();
}
function prevImage() {
stopGallery();
showPrev();
}
I'll leave it up to you to come up with a solution to that. 我将由您自己决定解决方案。
And you can also edit your showNext and showPrev functions with the addition of the following: 您还可以添加以下内容来编辑showNext和showPrev函数:
// add to showNext()
if (next.next().length===0) {
stopGallery();
nextBtn.addClass("hidden");
}
// add to showPrev()
if (prev.prev().length===0) {
prevBtn.addClass("hidden");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.