简体   繁体   中英

How to make multiple item slider repeat infinitely in jquery

So, I have this gallery which includes a slider. I want to make that slider loop infinitely but I can't seem to get the hang of it. I am a beginner in jQuery. I don't want to use Slick, I am already aware of its existence. I would like the images to repeat continuously, and also for the slider to stop moving when I'm hovering over it. But everything I try doesn't seem to work. This is the code:

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();
});

and this is the 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>

How would you go about it?

Thank you in advance!

For the slider to loop, try this:

Get max slide number:

var allslides = $('.slides li').length;

In your interval, check if currslides is over the last slide (over the line CurrSlides += 1; ). If it is, reset it to the first:

if (CurrSlides == allslides) currslides = 0
else CurrSlides += 1;

For the slider to stop on hover clear your interval on mouseover: https://api.jquery.com/mouseover/

Give your interval function a name first:

myintervalfunction = function() {
   $('#slider .slides').animate({ 'margin-left': '-=210' }, 10000,
    function () {
        CurrSlides += 1;
        if (CurrSlides === thumbnail_array.length-2) {
            $('#slider .slides').css('margin-left', '630');
        }
    });
}

Start the interval:

 setInterval( myinterval , 3000);

Stop on mouseover:

$( ".slide" ).mouseover(function() {
 clearInterval(myinterval );
});

Restart on mouseleave:

$( ".slide" ).mouseleave(function() {
  setInterval( myintervalfunction , 3000);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM