skip to Main Content

I have made an image slider.

enter image description here

Three problems:

  1. The last slide is showing. However the counter is showing the first slide. How do I solve this?

  2. Clicking play, plays the counter. How do I make it stop on the last slide?

  3. Clicking last, greys-out the next and last buttons. Also, clicking first, greys-out the prev and first buttons. I used CSS. How do I automate this with jQuery?

I have tried:

  1. here I have tried adding class .active to the last image and use css to hide the other images. Unfortunately the counter says 1/6 instead of 6/6.

  2. I have tried adding a function that checks if the current slide is the last slide. I don’t know why this is not working:

function lastSlide() {
        if ($('.image').last().hasClass('active')){
            clearTimeout(slideTimer);
        } else { 
            slideImage(0);
        };
    };
var totalImages = $(".images").length; 
        sliderCompleted: function(current) {
        if (slideImage >= totalImages) {

        clearTimeout(slideTimer);

    };
  1. I probably need to use $('.images:last') however I am having trouble understanding how to implement this.

I hope someone has the time to slide me in the right direction…

The HTML CSS & jQuery:

$(document).ready(function() {

// Slider

var slideTimer;

var slideImage = function(step) {

    if (step == undefined) step = 1;

    clearTimeout(slideTimer);

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

$('.nav-stop').click(function() {
    clearTimeout(slideTimer);
    $(this).hide();
    $('.nav-play').show();
    $('.nav-first').removeClass('nav-off');
    $('.nav-prev').removeClass('nav-off');
    $('.nav-next').removeClass('nav-off');
    $('.nav-last').removeClass('nav-off');
});

$('.nav-play').click(function() {
    slideImage(0);
    $(this).hide();
    $('.nav-stop').show();
    $('div.nav-wrap span').removeClass('nav-off');
});

$('.nav-prev').click(function() {
    slideImage(-1);
    clearTimeout(slideTimer);
    $('.nav-stop').hide();
    $('.nav-play').show();
    $('div.nav-wrap span').removeClass('nav-off');
});

$('.nav-next').click(function() {
    slideImage(1);
    clearTimeout(slideTimer);
    $('.nav-stop').hide();
    $('.nav-play').show();
    $('div.nav-wrap span').removeClass('nav-off');
    
    
});

$('.nav-first').click(function() {
   slideImage($('.image:first').index('.image') - $('.image:visible').index('.image'));
    clearTimeout(slideTimer);
    $('.nav-stop').hide();
    $('.nav-play').show();
    $('div.nav-wrap span').removeClass('nav-off');
    $('.nav-first').addClass('nav-off');
    $('.nav-prev').addClass('nav-off');
});


$('.nav-last').click(function() {
  slideImage($('.image:last').index('.image') - $('.image:visible').index('.image'));
    clearTimeout(slideTimer);
    $('.nav-stop').hide();
    $('.nav-play').show();
    $('div.nav-wrap span').removeClass('nav-off');
    $('.nav-next').addClass('nav-off');
    $('.nav-last').addClass('nav-off');
});



// Counter

var now = 0;

function updateCounter() {
    var slidesTotal = $('.image').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.active {
    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-stop {
    display: none;
}

div.slider div.nav-wrap span.nav-off {
    cursor: default;
    border: 1px solid silver;
    color: silver;
    background-color: whitesmoke;
    pointer-events: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<div class="slider">
        <figure class="image">
            <img src="http://www.bookmov.es/book/test_A.svg">
            <figcaption>Test A</figcaption>
        </figure>
        <figure class="image">
            <img src="http://www.bookmov.es/book/test_B.svg">
            <figcaption>Test B</figcaption>
        </figure>
        <figure class="image">
            <img src="http://www.bookmov.es/book/test_C.svg">
            <figcaption>Test C</figcaption>
        </figure>
        <figure class="image">
            <img src="http://www.bookmov.es/book/test_D.svg">
            <figcaption>Test D</figcaption>
        </figure>
        <figure class="image">
            <img src="http://www.bookmov.es/book/test_E.svg">
            <figcaption>Test E</figcaption>
        </figure>
        <figure class="image active">
            <img src="http://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>
    </div>

2

Answers


  1. $('.nav-first').click(function() {
        slider($('.images:first').index('.images') - $('.images:visible').index('.images'));
        clearTimeout(slideTimer);
        $('.nav-stop').hide();
        $('.nav-play').show();
    });
    
    
    $('.nav-last').click(function() {
        slider($('.images:last').index('.images') - $('.images:visible').index('.images'));
        clearTimeout(slideTimer);
        $('.nav-stop').hide();
        $('.nav-play').show();
    });
    
    let slider = function(step) {
        if (step == undefined) step = 1;
        clearTimeout(slideTimer);
        let look = $('.images:visible').index('.images');
        if (step != 0) {
            $('.images').stop();
            $('.images:visible').hide();
        }
        look = look + step;
        if (look >= $('.images').length) {
            look = 0;
            if (step > 0) {
                clearTimeout(slideTimer);
                $('.nav-stop').hide();
                $('.nav-play').show();
                updateCounter();
                return;
            }
        } else if (look < 0) {
            look = $('.images').length - 1;
        }
        $('.images:eq(' + look + ')').stop().show();
        if ($('.slider').length > 0 && step != 0) {
            slideTimer = setTimeout(slider, 2000);
        }
        now = look;
        updateCounter();
    };
    
    Login or Signup to reply.
  2. You just need to call slideImage(1); at the very end of the JS code in order to kick things off where you want them.

    $(document).ready(function() {
    
    // Slider
    
    var slideTimer;
    
    var slideImage = function(step) {
    
        if (step == undefined) step = 1;
    
        clearTimeout(slideTimer);
    
        var imageIndex = $('.image:visible').index('.image');
        
        if (step != 0) {
            $('.image').stop();
            $('.image:visible').hide();
        }
        imageIndex = imageIndex + step;
        if (imageIndex >= $('.image').length) {
            imageIndex = 0;
        } else if (imageIndex < 0) {
            imageIndex = $('.image').length - 1;
        }
        if (step != 0) {
            $('.image:eq(' + imageIndex + ')').stop().show();
        }
        if ($('.slider').length > 0) {
            slideTimer = setTimeout(slideImage, 2000);
        }
        now = imageIndex;
        updateCounter();
    };
    
    $('.nav-stop').click(function() {
        clearTimeout(slideTimer);
        $(this).hide();
        $('.nav-play').show();
        $('.nav-first').removeClass('nav-off');
        $('.nav-prev').removeClass('nav-off');
        $('.nav-next').removeClass('nav-off');
        $('.nav-last').removeClass('nav-off');
    });
    
    $('.nav-play').click(function() {
        slideImage(0);
        $(this).hide();
        $('.nav-stop').show();
        $('div.nav-wrap span').removeClass('nav-off');
    });
    
    $('.nav-prev').click(function() {
        slideImage(-1);
        clearTimeout(slideTimer);
        $('.nav-stop').hide();
        $('.nav-play').show();
        $('div.nav-wrap span').removeClass('nav-off');
    });
    
    $('.nav-next').click(function() {
        slideImage(1);
        clearTimeout(slideTimer);
        $('.nav-stop').hide();
        $('.nav-play').show();
        $('div.nav-wrap span').removeClass('nav-off');
    });
    
    $('.nav-first').click(function() {
       slideImage($('.image:first').index('.image') - $('.image:visible').index('.image'));
        clearTimeout(slideTimer);
        $('.nav-stop').hide();
        $('.nav-play').show();
        $('div.nav-wrap span').removeClass('nav-off');
        $('.nav-first').addClass('nav-off');
        $('.nav-prev').addClass('nav-off');
    });
    
    
    $('.nav-last').click(function() {
      slideImage($('.image:last').index('.image') - $('.image:visible').index('.image'));
        clearTimeout(slideTimer);
        $('.nav-stop').hide();
        $('.nav-play').show();
        $('div.nav-wrap span').removeClass('nav-off');
        $('.nav-next').addClass('nav-off');
        $('.nav-last').addClass('nav-off');
    });
    
    
    
    // Counter
    
    var now = 0;
    
    function updateCounter() {
        var slidesTotal = $('.image').length;
        $('.counter').text(now + 1 + '/' + slidesTotal);
    }
    updateCounter();
    slideImage(1);
    
    });
    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.active {
        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-stop {
        display: none;
    }
    
    div.slider div.nav-wrap span.nav-off {
        cursor: default;
        border: 1px solid silver;
        color: silver;
        background-color: whitesmoke;
        pointer-events: none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <div class="slider">
            <figure class="image">
                <img src="http://www.bookmov.es/book/test_A.svg">
                <figcaption>Test A</figcaption>
            </figure>
            <figure class="image">
                <img src="http://www.bookmov.es/book/test_B.svg">
                <figcaption>Test B</figcaption>
            </figure>
            <figure class="image">
                <img src="http://www.bookmov.es/book/test_C.svg">
                <figcaption>Test C</figcaption>
            </figure>
            <figure class="image">
                <img src="http://www.bookmov.es/book/test_D.svg">
                <figcaption>Test D</figcaption>
            </figure>
            <figure class="image">
                <img src="http://www.bookmov.es/book/test_E.svg">
                <figcaption>Test E</figcaption>
            </figure>
            <figure class="image active">
                <img src="http://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>
        </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search