100일의 코드 중 18일차

오늘은 HTML, Javascript 및 CSS를 사용하여 캐러셀을 만드는 방법을 배웠습니다. 캐러셀은 사용자가 슬라이드쇼에서 앞뒤로 이동하는 버튼을 클릭하여 선택할 수 있는 이미지 또는 정보의 컨테이너(슬라이드쇼)입니다.
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;
    }
}


자바스크립트

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");
}

좋은 웹페이지 즐겨찾기