I'm following the Codecademy "Make an interactive website" course. I don't have an error, but I had a question about something I don't understand: the if-statement used in a slide thing thing thing.
if (nextSlide.length === 0) {
nextSlide = $('.slide').first();
nextDot = $('.dot').first();
};
This if-statement is for that after the last slide it will go to the first slide.
Why is it nextSlide.length === 0
?
jQuery:
var main = function() {
$('.dropdown-toggle').click(function() {
$('.dropdown-menu').toggle();
});
$('.arrow-next').click(function() {
var currentSlide = $('.active-slide');
var nextSlide = currentSlide.next();
var currentDot = $('.active-dot');
var nextDot = currentDot.next();
if (nextSlide.length === 0) {
nextSlide = $('.slide').first();
nextDot = $('.dot').first();
};
currentSlide.fadeOut(600).removeClass('active-slide');
nextSlide.fadeIn(600).addClass('active-slide');
currentDot.removeClass('active-dot');
nextDot.addClass('active-dot');
});
};
$(document).ready(main);
The if
statement is there to do a check as to if there is a slide after the current slide or if it should revert to the first slide.
nextSlide.length
will return a number to represent if the next slide exists, else it will return 0.
.length
API Docs: Here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With