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

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

by ์ฝ”ํŒŒ์นด 2022. 8. 29.
728x90

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

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


HTML ์†Œ์Šค

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

<section id="sliderType02">
    <div class="slider__wrap">
        <div class="slider__img">
            <div class="slider__inner">
                <div class="slider">
                    <img src="../assets/img/effect_bg16-min.jpg" alt="์ด๋ฏธ์ง€1">
                </div>
                <div class="slider">
                    <img src="../assets/img/effect_bg17-min.jpg" alt="์ด๋ฏธ์ง€2">
                </div>
                <div class="slider">
                    <img src="../assets/img/effect_bg18-min.jpg" alt="์ด๋ฏธ์ง€3">
                </div>
                <div class="slider">
                    <img src="../assets/img/effect_bg19-min.jpg" alt="์ด๋ฏธ์ง€4">
                </div>
                <div class="slider">
                    <img src="../assets/img/effect_bg20-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: 4000px;
    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(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๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ , setInterval ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. 2์ดˆ๋งˆ๋‹ค ๋ณ€์ˆ˜์˜ ๊ฐ’์ด 1์”ฉ ์ปค์ง€๋˜, ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋“œ์˜ ์ด ๊ฐœ์ˆ˜๋งŒํผ ์ปค์ง€๋ฉด ๊ธฐ๋ณธ๊ฐ’(0)์—์„œ ๋‹ค์‹œ ๊ฐ’์ด ์ฆ๊ฐ€๋˜๋„๋ก ์ž‘์—…ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋“ค์—๋Š” transition์œผ๋กœ ์›€์ง์ž„ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ฃผ์—ˆ๊ณ , ์ด๋ฏธ์ง€์˜ ๋„ˆ๋น„๊ฐ€ 800px์ด๋ฏ€๋กœ ์ขŒ์ธก์œผ๋กœ 800px์”ฉ ์›€์ง์ด๋„๋ก ์ž‘์—…ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

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;  // ์ด๋ฏธ์ง€ ๊ฐœ์ˆ˜
    
sliderInner.style.transition = "all 0.6s";
    
// sliderInner.style.transform += "translateX(0px)";        800 * 0
// 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
    
setInterval(() => {  // 2000 : 2์ดˆ์— ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    // currentIndex++;
    // currentIndex์˜ ๊ฐ’์ด ์ด๋ฏธ์ง€ ๊ฐœ์ˆ˜๋ฅผ ์ดˆ๊ณผํ•˜๋ฉด, ๊ฐ’์„ ๋ฆฌ์…‹์‹œํ‚ค๋„๋ก ํ•จ
    
    // 1. if ์กฐ๊ฑด๋ฌธ์„ ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•
    // if(currentIndex < slider.length-1){
    //     currentIndex++;
    // } else {
    //     currentIndex = 0;
    // }
    
    // 2. ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•(๊ถŒ์žฅ๋˜์ง€๋Š” ์•Š์Œ)
    // (currentIndex < 4) ? currentIndex++ : currentIndex = 0;
    
    // 3. ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ํ•˜๊ธฐ
    currentIndex = (currentIndex + 1) % sliderCount;  // 1 2 3 4 0 1 2 3 4
    
    sliderInner.style.transform = "translateX("+ -800 * currentIndex +"px)";
}, 2000);

JQUERY ์†Œ์Šค

์ด๋ฒˆ์—๋Š” JQUERY ์†Œ์Šค๋กœ๋„ ์ž‘์—…ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. JQUERY๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํ‘œํ˜„ํ•˜๋Š” ์‹์„ ๋น„๊ต์  ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์— ๋น„ํ•ด ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋ถ€๋ถ„์€ ๋‹ค์†Œ ๋ณต์žกํ–ˆ์Šต๋‹ˆ๋‹ค.

let currentIndex = 0;

// ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(์ขŒ๋ณ€)์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž‘์„ฑํ•˜๋Š” ์‹์„, ์ œ์ด์ฟผ๋ฆฌ(์šฐ๋ณ€)์—์„œ๋Š” ๊ฐ„๋‹จํžˆ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Œ
// document.querySelector(".slider__inner") = $(".slider__inner")
setInterval(() => {
    currentIndex = (currentIndex + 1) % $(".slider").length;
    
    // $(".slider__inner").animate({
    //     transform : "translateX("+ -800 * currentIndex +")"
    // }, 2000);

    $(".slider__inner").css("position", "relative");  // css์— ์Šคํƒ€์ผ ์ถ”๊ฐ€ํ•˜๊ธฐ
    $(".slider__inner").animate({ left: -800 * currentIndex }, 600)
}, 2000);

GSAP ์†Œ์Šค

์ด๋ฒˆ์—๋Š” GSAP ์†Œ์Šค๋กœ๋„ ์ž‘์—…ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. GSAP๋ฅผ ํ™œ์šฉํ•˜๋ฉด, ๊ฐ„๋‹จํ•œ ์†Œ์Šค๋กœ ๋ถ€๋“œ๋Ÿฌ์šด ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const slider = document.querySelectorAll(".slider");
let currentIndex = 0;
    
setInterval(() => {
    currentIndex = (currentIndex + 1) % slider.length;
 
    gsap.to(".slider__inner", {
        x: -800 * currentIndex,
    });
}, 2000);

๊ฒฐ๊ณผ

728x90

๋Œ“๊ธ€

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

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