๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ดํŽ™ํŠธ

03. ์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ - ์ขŒ๋กœ ์›€์ง์ด๊ธฐ ์—ฐ์†

by Youcodein 2022. 9. 2.
728x90
๋ฐ˜์‘ํ˜•

์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ 03

์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ ์„ธ ๋ฒˆ์งธ, ์ขŒ๋กœ ์›€์ง์ด๊ธฐ(์—ฐ์†์ ์œผ๋กœ) ์ž…๋‹ˆ๋‹ค.

HTML

html ์†Œ์Šค์ž…๋‹ˆ๋‹ค.

<section id="sliderType02">
    <div class="slider__wrap">
        <div class="slider__img">
            <div class="slider__inner">
                <div class="slider"><img src="../effect/img/effect_sub01-min.jpg" alt=""></div>
                <div class="slider"><img src="../effect/img/effect_sub02-min.jpg" alt=""></div>
                <div class="slider"><img src="../effect/img/effect_sub03-min.jpg" alt=""></div>
                <div class="slider"><img src="../effect/img/effect_sub04-min.jpg" alt=""></div>
                <div class="slider"><img src="../effect/img/effect_sub05-min.jpg" alt=""></div>
            </div>
        </div>
    </div>
</section>

CSS

css ์†Œ์Šค์ž…๋‹ˆ๋‹ค.

/* slider */
.slider__wrap {
    width: 100%;
    height: 100vh;
    display: flex;

    align-items: center;
    justify-content: center;

}

/* ์ด๋ฏธ์ง€ ๋ณด์ด๋Š” ์˜์—ญ */
.slider__img {
    position: relative;
    width: 800px;
    height: 450px;
    overflow: hidden;

}


.slider::before {
    position: absolute;
    left: 5px;
    top: 5px;
    background-color: rgba(0, 0, 0, 0.4);
    color: #fff;
    padding: 5px 10px;
}


.slider img {
    width: 100%;

}


.slider__inner {
    /* ์ด๋ฏธ์ง€๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ๋ถ€๋ชจ : ์›€์ง์ด๋Š” ๋ถ€๋ถ„ */
    display: flex;
    flex-wrap: wrap;
    width: 4800px;
    /*์ด ์ด๋ฏธ์ง€ 6๊ฐœ */
    height: 450px;
}



.slider {
    /* ๊ฐœ๋ณ„์ ์ธ ์ด๋ฏธ์ง€ */
    position: relative;
    width: 800px;
    height: 450px;
}

.slider:nth-child(1)::before {
    content: '์ด๋ฏธ์ง€1';
}

.slider:nth-child(2)::before {
    content: '์ด๋ฏธ์ง€2';
}

.slider:nth-child(3)::before {
    content: '์ด๋ฏธ์ง€3';
}

.slider:nth-child(4)::before {
    content: '์ด๋ฏธ์ง€4';
}

.slider:nth-child(5)::before {
    content: '์ด๋ฏธ์ง€5';
}

.slider:nth-child(6)::before {
    content: '์ด๋ฏธ์ง€1';
}


@media (max-width: 800px) {
    .slider__img {
        width: 400px;
        height: 225px;
    }
}

Javascript

Javascript ์†Œ์Šค์ž…๋‹ˆ๋‹ค.

const sliderWrap = document.querySelector(".slider__wrap");
const sliderImg = document.querySelector(".slider__img");           //๋ณด์—ฌ์ง€๋Š” ์˜์—ญ
const sliderInner = document.querySelector(".slider__inner");       //์›€์ง์ด๋Š” ์˜์—ญ
const slider = document.querySelectorAll(".slider");                //๊ฐ๊ฐ์˜ ์ด๋ฏธ์ง€


let currentIndex = 0;                                                   //ํ˜„์žฌ ๋ณด์ด๋Š” ์ด๋ฏธ์ง€
let sliderCount = slider.length;                                        //์ด๋ฏธ์ง€ ๊ฐฏ์ˆ˜
let sliderWidth = sliderImg.offsetWidth;                                //์ด๋ฏธ์ง€ ๊ฐ€๋กœ๊ฐ’
let sliderClone = sliderInner.firstElementChild.cloneNode(true);        //์ฒซ๋ฒˆ์งธ ์ด๋ฏธ์ง€ ๋ณต์‚ฌ
sliderInner.appendChild(sliderClone);                                   //์ฒซ๋ฒˆ์งธ ์ด๋ฏธ์ง€๋ฅผ ๋งˆ์ง€๋ง‰์— ๋„ฃ์–ด์คŒ

function sliderEffect() {
    currentIndex++;
    sliderInner.style.transition = "all 0.6s";
    sliderInner.style.transform = "translateX(-" + sliderWidth * currentIndex + "px)";

    // sliderInner.style.transform = "translateX(-800px)";  800 * 1
    // sliderInner.style.transform = "translateX(-1600px)";  800 * 2
    // sliderInner.style.transform = "translateX(-2400px)";  800 * 3
    // sliderInner.style.transform = "translateX(-3200px)";  800 * 4
    // sliderInner.style.transform = "translateX(-4000px)";  800 * 5
    // sliderInner.style.transform = "translateX(-4800px)";  800 * 6

    // ๋งˆ์ง€๋ง‰ ์‚ฌ์ง„์— ์œ„์น˜ํ–ˆ์„ ๋•Œ
    if (currentIndex == sliderCount) {
        setTimeout(() => {
            sliderInner.style.transition = "0s";
            sliderInner.style.transform = "translateX(0px)";
        }, 700);
        currentIndex = 0;
    }
}
setInterval(sliderEffect, 2000);
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€