scrimba
JavaScript Challenges
Build a Carousel: Hiding non-active slides
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Build a Carousel: Hiding non-active slides
AboutCommentsNotes
Build a Carousel: Hiding non-active slides
Expand for more info
index.js
run
preview
console
/*
Hiding non-active slides

1) In our 'styles.css', add a new class 'carousel-item-hidden' that is set to display none
2) Create a new function called 'hideAllSlides'
3) Inside 'hideAllSlides' use a 'for of loop' to iterate through the slides (each iteration will give you direct access to 'carousel-item')
4) When inside the 'for of loop', remove the class 'carousel-item-visible' and add the class 'carousel-item-hidden' - all our slides will now be hidden, and inside 'moveToNextSlide' at the end, we add back the slide we want visible!
5) Call 'hideAllSlides' right away within the function 'moveToNextSlide' - make sure it's before any other code!
*/

const slides = document.getElementsByClassName('carousel-item');
let slidePosition = 0;
const totalSlides = slides.length;

document.getElementById('carousel-button-next').addEventListener('click', moveToNextSlide);
document.getElementById('carousel-button-prev').addEventListener('click', moveToPrevSlide);

function moveToNextSlide() {
if (slidePosition === totalSlides - 1) {
slidePosition = 0;
} else {
slidePosition++;
}

slides[slidePosition].classList.add("carousel-item-visible");
}

function moveToPrevSlide() {

}
Console
/index.html
-5:37