所以,我有这个包含滑块的画廊。我想无限制地制作滑块循环,但我似乎无法掌握它。我是jQuery的初学者。我不想使用Slick,我已经意识到它的存在。我希望图像能够连续重复,并且当我将鼠标悬停在滑块上时滑块也会停止移动。但我尝试的一切似乎都不起作用。这是代码:
function set_body_height() {
}
$(document).ready(function () {
var thumbnail_array = $(".slides img").map(function () {
return $(this).attr("src");
}).get();
$('#MainImage').attr('src', thumbnail_array[0]);
$('#Previous').css("visibility", "hidden");
var lengthImages = thumbnail_array.length;
var CurrImage = 0;
var widthImg = 210;
var BottomLength = 1;
var IndexDiff;
$('.slides li img').click(function () {
var Imagesrc = $(this).attr('src');
var ImageIndex = $(this).parent('.slide').index();
$('#MainImage').fadeOut('slow', function () {
if (ImageIndex <= lengthImages - BottomLength) {
IndexDiff = CurrImage;
CurrImage = ImageIndex;
IndexDiff = Math.abs(IndexDiff - CurrImage);
$('#slider .slides').animate({ 'margin-left': '-=' + widthImg * IndexDiff }, 4000);
$('#MainImage').attr('src', Imagesrc);
if (ImageIndex != 0) {
if ($('#Previous').css("visibility") == "hidden") {
$('#Previous').css("visibility", "visible");
}
}
if (ImageIndex == lengthImages - BottomLength) {
if ($('#Next').css("visibility") == "visible") {
$('#Next').css("visibility", "hidden");
}
}
}
else {
$('#MainImage').attr('src', Imagesrc);
}
}).fadeIn(3000);
});
$('#Next').click(function () {
$('#MainImage').fadeOut('slow', function () {
CurrImage = CurrImage + 1;
if (CurrImage <= lengthImages - BottomLength) {
$('#slider .slides').animate({ 'margin-left': '-=' + widthImg }, 000);
}
$('#MainImage').attr('src', thumbnail_array[CurrImage]);
if (CurrImage == lengthImages - BottomLength) {
$('#Next').css("visibility", "hidden");
}
if ($('#Previous').css("visibility") == "hidden") {
$('#Previous').css("visibility", "visible");
}
}).fadeIn(3000);
$('.slides li:last').after($('.slides li:first'));
});
$('#Previous').click(function () {
$('#MainImage').fadeOut('slow', function () {
CurrImage = CurrImage - 1;
$('#slider .slides').animate({ 'margin-left': '+=' + widthImg }, 4000);
$('#MainImage').attr('src', thumbnail_array[CurrImage]);
if (CurrImage == 0) {
$('#Previous').css("visibility", "hidden");
}
if ($('#Next').css("visibility") == "hidden") {
$('#Next').css("visibility", "visible");
}
$('.slides li:first').after($('.slides li:last'));
}).fadeIn(1000);
});
$('.slides li img').hover(function () {
$(this).css('cursor', 'pointer');
$(this).css({ 'filter': 'alpha(opacity=50)', 'opacity': '0.5' });
}, function () {
$(this).css('cursor', 'none');
$(this).css({ 'filter': 'alpha(opacity=100)', 'opacity': '1' });
});
var CurrSlides = 0;
setInterval(function () {
$('#slider .slides').animate({ 'margin-left': '-=210' }, 10000,
function () {
CurrSlides += 1;
if (CurrSlides === thumbnail_array.length-2) {
$('#slider .slides').css('margin-left', '630');
}
});
}, 3000);
//the images don't go over the slider
function set_body_height() {
$('#mainpic img').height($(window).height());
}
$(document).ready(function() {
$(window).bind('resize', set_body_height);
set_body_height();
});
这是html:
<div>
<div id="MainDiv">
<div id="mainpic">
<img src="img/pic1.jpg" alt="London" id="MainImage" width="100%"/></div>
<div id="child">
<img id="Next" src="img/rightarrow.png" class="NextButton" style="height: 50px;"/>
<img id="Previous" src="img/leftarrow.png" class="PreButton" style="height: 50px;"/>
</div>
</div>
<div id="slider">
<ul class="slides">
<li class="slide">
<img src="img/pic1.jpg" alt="London" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic2.jpg" alt="Florence" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic3.jpeg" alt="Amsterdam" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic4.jpg" alt="Venice" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic5.jpg" alt="Prague" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic6.jpg" alt="Krakow" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic7.jpg" alt="Vienna" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic8.jpg" alt="Szczecin" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic9.jpg" alt="Thassos" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic10.jpg" alt="Budapest" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic11.jpg" alt="Pisa" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic12.jpg" alt="Verona" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic13.jpg" alt="Ljubljana" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic14.jpg" alt="Bratislava" width="200" height="120"/>
</li>
<li class="slide">
<img src="img/pic15.jpg" alt="Zakopane" width="200" height="120"/>
</li>
</ul>
</div>
</div>
你会怎么做?
提前谢谢!
答案 0 :(得分:0)
要使滑块循环,请尝试以下操作:
获取最大幻灯片编号:
var allslides = $('.slides li').length;
在您的间隔中,检查currslides是否在最后一张幻灯片上方(在CurrSlides += 1;
行上方)。如果是,请将其重置为第一个:
if (CurrSlides == allslides) currslides = 0
else CurrSlides += 1;
要使滑块停留在悬停状态,请清除鼠标悬停时间间隔:https://api.jquery.com/mouseover/
首先为您的间隔函数命名:
myintervalfunction = function() {
$('#slider .slides').animate({ 'margin-left': '-=210' }, 10000,
function () {
CurrSlides += 1;
if (CurrSlides === thumbnail_array.length-2) {
$('#slider .slides').css('margin-left', '630');
}
});
}
开始间隔:
setInterval( myinterval , 3000);
鼠标悬停时停止:
$( ".slide" ).mouseover(function() {
clearInterval(myinterval );
});
重新启动mouseleave:
$( ".slide" ).mouseleave(function() {
setInterval( myintervalfunction , 3000);
});