javascript - Why do I have to check for length === 0 to restart the slides? -
i'm following codecademy "make interactive website" course. don't have error, had question don't understand: if-statement used in slide thing thing thing.
if (nextslide.length === 0) { nextslide = $('.slide').first(); nextdot = $('.dot').first(); };
this if-statement after last slide go first slide. why 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 there check if there slide after current slide or if should revert first slide.
nextslide.length
return number represent if next slide exists, else return 0.
.length
api docs: here
Comments
Post a Comment