๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Javascript Effect

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์Šฌ๋ผ์ด๋“œ ํšจ๊ณผ : ์ขŒ๋กœ ์›€์ง์ด๋Š” ์œ ํ˜•(์—ฐ์†์ )

by ์ฝ”ํŒŒ์นด 2022. 9. 1.
728x90

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์Šฌ๋ผ์ด๋“œ ํšจ๊ณผ : ์ขŒ๋กœ ์›€์ง์ด๋Š” ์œ ํ˜•(์—ฐ์†์ )

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋‹ค์–‘ํ•œ ํšจ๊ณผ๋“ค ์ค‘, CSS์˜ ํŠธ๋žœ์ง€์…˜ ์†์„ฑ์„ ํ™œ์šฉํ•œ ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋“œ ํšจ๊ณผ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์œ ํ˜•์€ ๋งˆ์ง€๋ง‰ ์ด๋ฏธ์ง€์— ๋„๋‹ฌํ•ด๋„, ์ฒ˜์Œ ์ด๋ฏธ์ง€๋กœ ๋Œ์•„๊ฐ€์ง€ ์•Š๊ณ  ์—ฐ์†์ ์œผ๋กœ ์ด๋ฏธ์ง€๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ์œ ํ˜•์ž…๋‹ˆ๋‹ค.


HTML ์†Œ์Šค

HTML๋กœ ๋ผˆ๋Œ€๋ฅผ ๋จผ์ € ์žก์•„์ค๋‹ˆ๋‹ค. section ํƒœ๊ทธ ์•ˆ์—๋Š” ํด๋ž˜์Šค๋ช…์ด slider__wrap์ธ div ๋ฐ•์Šค, ๊ทธ ์•ˆ์—๋Š” ํด๋ž˜์Šค๋ช…์ด slider__img์ธ div ๋ฐ•์Šค๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๊ณ , ๊ทธ ์•ˆ์—๋Š” slider__inner ๋ฐ•์Šค, ๊ทธ ์•ˆ slider ๋ฐ•์Šค ์•ˆ์—๋Š” ๊ฐ๊ฐ ์ด๋ฏธ์ง€ 5์žฅ์„ ์‚ฝ์ž…ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ div ๋ฐ•์Šค์˜ ์šฉ๋„๋Š” CSS ์†Œ์Šค์—์„œ ์ถ”๊ฐ€๋กœ ์„ค๋ช…ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ถ๊ธˆํ•˜๋ฉด ๋ณด์‹œ๋“ ๊ฐ€...

<section id="sliderType03">
    <div class="slider__wrap">
        <div class="slider__img">
            <div class="slider__inner">
                <div class="slider">
                    <img src="../assets/img/effect_bg11-min.jpg" alt="์ด๋ฏธ์ง€1">
                </div>
                <div class="slider">
                    <img src="../assets/img/effect_bg12-min.jpg" alt="์ด๋ฏธ์ง€2">
                </div>
                <div class="slider">
                    <img src="../assets/img/effect_bg13-min.jpg" alt="์ด๋ฏธ์ง€3">
                </div>
                <div class="slider">
                    <img src="../assets/img/effect_bg14-min.jpg" alt="์ด๋ฏธ์ง€4">
                </div>
                <div class="slider">
                    <img src="../assets/img/effect_bg15-min.jpg" alt="์ด๋ฏธ์ง€5">
                </div>
                </div>
            </div>
        </div>
    </div>
</section>

CSS ์†Œ์Šค

/* slider */
.slider__wrap {
    width: 100%;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}
.slider__img { /* ์ด๋ฏธ์ง€ ๋ณด์ด๋Š” ์˜์—ญ */
    position: relative;
    width: 800px;
    height: 520px;
    overflow: hidden;
}
.slider__inner { /* ์ด๋ฏธ์ง€๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ๋ถ€๋ชจ(์›€์ง์ด๋Š” ๋ถ€๋ถ„) */
    display: flex;
    flex-wrap: wrap;
    width: 4800px;
    height: 520px;
}
.slider { /* ๊ฐœ๋ณ„์ ์ธ ์ด๋ฏธ์ง€ */
    position: relative;
    width: 800px;
    height: 520px;
}
.slider::before{
    position: absolute;
    left: 5px;
    top: 5px;
    background: rgba(0,0,0,0.4);
    color: #fff;
    padding: 5px 10px;
}

.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';}
.slider:nth-child(1) {z-index: 5;}
.slider:nth-child(2) {z-index: 4;}
.slider:nth-child(3) {z-index: 3;}
.slider:nth-child(4) {z-index: 2;}
.slider:nth-child(5) {z-index: 1;}

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

JAVASCRIPT ์†Œ์Šค

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ๋Š” ๋จผ์ € ์„ ํƒ์ž๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์ดํ›„ currentIndex๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ , cloneNode() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ฒซ ๋ฒˆ์งธ ์ด๋ฏธ์ง€๊ฐ€ ๋งˆ์ง€๋ง‰์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ์ž‘์—…ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์ดํ›„, setInterval์„ ์‚ฌ์šฉํ•˜์—ฌ 2์ดˆ๋งˆ๋‹ค currentIndex์˜ ๊ฐ’์ด 1์”ฉ ์ฆ๊ฐ€ํ•˜๋„๋ก ํ•˜๊ณ , ํ•ด๋‹น ๊ฐ’์ด ์ด ์ด๋ฏธ์ง€์˜ ๊ฐœ์ˆ˜์— ๋„๋‹ฌํ•˜๋ฉด ์ฒ˜์Œ์œผ๋กœ ๋Œ์•„๊ฐ€๋„๋ก ์ž‘์—…ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋Š” ๋ถ„๋ช… ์ฒ˜์Œ์œผ๋กœ ๋‹ค์‹œ ๋Œ์•„๊ฐ€๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ทจํ•˜์ง€๋งŒ, ์†๋„๋ฅผ ๋น ๋ฅด๊ฒŒ ์กฐ์ ˆํ•จ์œผ๋กœ์จ ์—ฐ์†์ ์œผ๋กœ ์›€์ง์ด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ํ•˜๋Š” ํšจ๊ณผ์ž…๋‹ˆ๋‹ค.

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

๋Œ“๊ธ€

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๐Ÿฆ™

CSS
๊ด‘๊ณ  ์ค€๋น„์ค‘