【发布时间】:2016-04-08 02:38:24
【问题描述】:
我有以下示例,它遍历每个 LI,如果您将鼠标悬停,图像将暂停并在您鼠标移出时继续,但这是我所掌握的。
我希望它在到达最后一个 LI 时停止,并且还想让我添加的 2 个链接带有“下一个”和“上一个”类,所以如果单击它将移动到下一个或上一个图像。当然,如果在第一张图片上设置一个类,比如隐藏,如果在最后一张图片上设置下一个按钮隐藏所以不能使用,但是这是我所得到的。
$( 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>
【问题讨论】:
标签: javascript jquery html css