Day 18 of 100 Days of Code
Tags: javascript, webdev, beginners, programming
Published: 2022-04-25 19:43:14

Today I learnt how to build a carousel using HTML, Javascript and CSS. A carousel is a container (slideshow) of images or info that users can select by clicking a button that directs them forward or backward in the slideshow.
HTML

```
<html>
    <head><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.css">
        <link rel="stylesheet" href="styles.css">
    </head>
    <body class="container">
        <div class="carousel">
            <div class="carousel-item carousel-item-visible">
                <img src="images/1.jpg" />
            </div>
            <div class="carousel-item">
                <img src="images/2.jpg" />
            </div>
            <div class="carousel-item">
                <img src="images/3.jpg" />
            </div>
            
            <div class="carousel-actions">
                <button id="carousel-button-prev" aria-label="Previous Slidex"><</button>
                <button id="carousel-button-next" aria-label="Next Slide">></button>
            </div>
        </div>
    
        <script src="index.js"></script>
    </body>
</html>
``` 
CSS

```

body{
    margin:0;
    background-color: rgba(77, 66, 71, 0.21);
    
}
.container{
    display:flex;
   
    align-items:center;
    justify-content: center;
}
.carousel {
    max-width: 600px;
    position: relative;
    
}

.carousel .carousel-item,
.carousel .carousel-item-hidden {
    display: none;
}

.carousel .carousel-item-visible {
    display: block;
    animation: fadeVisibility 0.5s;
}

.carousel .carousel-item img {
    width: 100%;
    max-width: 600px;
    height: auto;
}

.carousel .carousel-actions {
    display: flex;
    width: 100%;
    justify-content: space-between;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
}

.carousel .carousel-actions button {
    border-radius: 50px;
    background-color: white;
    border: 0;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    width: 40px;
    height: 40px;
}

.carousel .carousel-actions button#carousel-button-prev {
    margin-left: 20px;
}

.carousel .carousel-actions button#carousel-button-next {
    margin-right: 20px;   
}

@keyframes fadeVisibility {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}
``` 
Javascript

```
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 hideAllSlides() {
    for (let slide of slides) {
        slide.classList.remove('carousel-item-visible');
        slide.classList.add('carousel-item-hidden');
    }
}

function moveToNextSlide() {
    hideAllSlides();
    
    if (slidePosition === totalSlides - 1) {
        slidePosition = 0;
    } else {
        slidePosition++;
    }
    
    slides[slidePosition].classList.add("carousel-item-visible");
}

function moveToPrevSlide() {
    hideAllSlides();
    
    if (slidePosition === 0) {
        slidePosition = totalSlides - 1;
    } else {
        slidePosition--;
    }
    
    slides[slidePosition].classList.add("carousel-item-visible");
}
```