How to improve this slider’s navigation (Jump to First, Last and stop Loop)?

  Kiến thức lập trình

I’m working with an image slider.

Successfully implemented:

  1. auto-play
  2. stop / start button
  3. next slide
  4. previous slide
  5. slide counter

What I am having trouble with:

  1. click ‘first’ to show first slide
  2. click ‘last’ to show last slide
  3. automatically stop slider at last slide

What I have tried:

  1. I have tried lastIndexOf(); and array.lastIndexOf(item, start) and findLastIndex() however I do not understand how to properly build these in my current jQuery. I keep breaking the slider.

  2. I have tried to stop the slider form looping, as I believe the slider will then stop at the last slide. I tried removing if (step != 0) however no luck there either.

Please could someone slide me in the right direction?

The code:

$(document).ready(function() {
    
    // Slider

    var slideTimer;
    var slider = function(step) {
        if (step == undefined) step = 1;
        clearTimeout(slideTimer);
        var look = $('.images:visible').index('.images');
        if (step != 0) {
            $('.images').stop();
            $('.images:visible').hide();
        }
        look = look + step;
        if (look >= $('.images').length) {
            look = 0;
        } else if (look < 0) {
            look = $('.images').length - 1;
        }
        if (step != 0) {
            $('.images:eq(' + look + ')').stop().show();
        }
        if ($('.slider').length > 0) {
            slideTimer = setTimeout(slider, 2000);
        }
        now = look;
        updateCounter();
    };
    slider(0);

    $('.nav-stop').click(function() {
        clearTimeout(slideTimer);
        $(this).hide();
        $('.nav-play').show();
    });

    $('.nav-play').click(function() {
        slider(0);
        $(this).hide();
        $('.nav-stop').show();
    });

    $('.nav-prev').click(function() {
        slider(-1);
        clearTimeout(slideTimer);
        $('.nav-stop').hide();
        $('.nav-play').show();
    });

    $('.nav-next').click(function() {
        slider(1);
        clearTimeout(slideTimer);
        $('.nav-stop').hide();
        $('.nav-play').show();
    });

    $('.nav-first').click(function() {
        slider(0);
        clearTimeout(slideTimer);
        $('.nav-stop').hide();
        $('.nav-play').show();
    });

    $('.nav-last').click(function() {
        slider(0);
        clearTimeout(slideTimer);
        $('.nav-stop').hide();
        $('.nav-play').show();
    });

    
    // Counter

    var now = 0;

    function updateCounter() {
        var slidesTotal = $('.images').length;
        $('.counter').text(now + 1 + '/' + slidesTotal);
    }
    updateCounter();

});
div.slider {
    position: relative;
    float: left;
    padding: 10px;
    border: 1px solid black;
    outline: none;
    margin: 0;
    box-sizing: border-box;
}

div.slider figure {
    position: relative;
    display: none;
    width: 300px;
    padding: 10px;
    border: 1px solid black;
    outline: none;
    margin: 0 0 10px 0;
    box-sizing: border-box;
}

div.slider figure.first {
    display: block;
}

div.slider figure img {
    position: relative;
    width: 100%;
    padding: 10px;
    border: 1px solid black;
    outline: none;
    margin: 0 0 10px 0;
    box-sizing: border-box;
}

div.slider figcaption {
    position: relative;
    padding: 10px;
    border: 1px solid black;
    outline: none;
    margin: 0;
    box-sizing: border-box;
    font-size: 16px;
    line-height: 20px;
}

div.slider div.nav-wrap {
    position: relative;
    display: inline-block;
    width: 300px;
    padding: 10px;
    border: 1px solid black;
    outline: none;
    margin: 0;
    box-sizing: border-box;
    font-size: 16px;
    line-height: 20px;
}

div.slider div.nav-wrap span {
    position: relative;
    float: left;
    width: 20%;
    height: 40px;
    padding: 10px;
    border: 1px solid black;
    outline: none;
    margin: 0;
    box-sizing: border-box;
    font-size: 16px;
    line-height: 20px;
    text-align: center;
    color: black;
    background-color: white;
}

div.slider div.nav-wrap span:hover:not(.counter) {
    cursor: pointer;
    border: 1px solid silver;
    color: silver;
    background-color: whitesmoke;
}

div.slider div.nav-wrap span.counter {
    width: 100%;
    margin: 10px 0 0 0;
}

div.slider div.nav-wrap span.nav-play {
    display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div class="slider">
        <figure class="images first">
            <img src="https://www.bookmov.es/book/test_A.svg">
            <figcaption>Test A</figcaption>
        </figure>
        <figure class="images">
            <img src="https://www.bookmov.es/book/test_B.svg">
            <figcaption>Test B</figcaption>
        </figure>
        <figure class="images">
            <img src="https://www.bookmov.es/book/test_C.svg">
            <figcaption>Test C</figcaption>
        </figure>
        <figure class="images">
            <img src="https://www.bookmov.es/book/test_D.svg">
            <figcaption>Test D</figcaption>
        </figure>
        <figure class="images">
            <img src="https://www.bookmov.es/book/test_E.svg">
            <figcaption>Test E</figcaption>
        </figure>
        <figure class="images">
            <img src="https://www.bookmov.es/book/test_F.svg">
            <figcaption>Test F</figcaption>
        </figure>
        <div class="nav-wrap">
            <span class="nav-stop">stop</span>
            <span class="nav-play">play</span>
            <span class="nav-first">first</span>
            <span class="nav-prev">prev</span>
            <span class="nav-next">next</span>
            <span class="nav-last">last</span>
            <span class="counter"></span>
        </div>

LEAVE A COMMENT