I have made an image slider.
Three problems:
-
The last slide is showing. However the counter is showing the first slide. How do I solve this?
-
Clicking play, plays the counter. How do I make it stop on the last slide?
-
Clicking last, greys-out the
next
andlast
buttons. Also, clicking first, greys-out theprev
andfirst
buttons. I used CSS. How do I automate this with jQuery?
I have tried:
-
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.
-
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);
};
- 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
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.