๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
EFFECT

[EFFCET] ์ด๋ฏธ์ง€ : ์Šฌ๋ผ์ด๋“œ ์œ ํ˜•03

by _ํ† ๋งคํ†  2022. 9. 2.
728x90

์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋“œ : ์—ฐ์†์ ์œผ๋กœ ์›€์ง์ด๊ธฐ

์ง€๋‚œ ์‹œ๊ฐ„์—๋Š” ์Šฌ๋ผ์ด๋“œ๊ฐ€ ์ขŒ์ธก์œผ๋กœ ์ด๋™ํ•˜๋Š” ํƒ€์ž…์œผ๋กœ ๋งˆ์ง€๋ง‰์—์„œ ์Šˆ๋ฃจ๋ฃฉ~ ์ฒซ๋ฒˆ์งธ๋กœ ๋Œ์•„๊ฐ”๋‹ค๋ฉด
์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์ด๋ฏธ์ง€๊ฐ€ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋„˜์–ด๊ฐ€๋„๋ก 2์ดˆ ์ง€๋‚  ๋•Œ๋งˆ๋‹ค ์ด๋ฏธ์ง€๊ฐ€ ์—ฐ์†์ ์œผ๋กœ ๋ฐ”๋€Œ๋Š” ์Šฌ๋ผ์ด๋“œ ํ˜•์‹์˜ ํŽ˜์ด์ง€๋ฅผ ์ž‘์—…ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜€


#1. HTML / CSS ์„ค์ •ํ•˜๊ธฐ

! HTML TIP !
๊ฐ ์ด๋ฏธ์ง€๋งˆ๋‹ค css์„ค์ •์„ ์ฃผ๊ธฐ ์œ„ํ•ด ๋”ฐ๋กœ ๋ถ€๋ชจ๋ฅผ 2๋ฒˆ ๊ฐ์‹ธ์„œ ๋ฌถ์–ด์ค๋‹ˆ๋‹ค.

โœ๏ธ HTML ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ
<section id="sliderType03">
    <div class="slider__wrap">
        <div class="slider__img">
            <div class="slider__inner">
                <div class="slider">
                    <img src="../assets/img/Mountian_bg01.jpg" alt="์ด๋ฏธ์ง€6">
                </div>
                <div class="slider">
                    <img src="../assets/img/Mountian_bg02.jpg" alt="์ด๋ฏธ์ง€7">
                </div>
                <div class="slider">
                    <img src="../assets/img/Mountian_bg03.jpg" alt="์ด๋ฏธ์ง€8">
                </div>
                <div class="slider">
                    <img src="../assets/img/Mountian_bg04.jpg" alt="์ด๋ฏธ์ง€9">
                </div>
                <div class="slider">
                    <img src="../assets/img/Mountian_bg05.jpg" alt="์ด๋ฏธ์ง€10">
                </div>
            </div>
        </div>
    </div>
</section>

! CSS TIP !
1. ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€๋กœ๋กœ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ํฌ์ง€์…˜์„ relative๋กœ ์„ค์ •ํ•˜๊ณ  ์˜์—ญ์—์„œ ๋ฒ—์–ด๋‚˜๋Š” ์ด๋ฏธ์ง€๋ฅผ ์•ˆ๋ณด์ด๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด overflow: hidden์„ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.
2. ์ด๋ฏธ์ง€๊ฐ€ ์—ฐ์†์ ์œผ๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๋„๋ก ์ฒซ๋ฒˆ์งธ ์ด๋ฏธ์ง€๋ฅผ 6๋ฒˆ์งธ์— ์ถ”๊ฐ€๋กœ ๋„ฃ์Šต๋‹ˆ๋‹ค. (์• ๋‹ˆ๋ฉ”์ด์…˜ ์„ค์ •์€ ์Šคํฌ๋ฆฝํŠธ ํŒŒํŠธ์—์„œ!!)
3. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๋ฏธ์ง€๊ฐ€ 6์žฅ์ด๊ธฐ ๋•Œ๋ฌธ์— inner์˜ ๊ฐ€๋กœ ํฌ๊ธฐ๋ฅผ +ํ•˜์—ฌ 4800px๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

โœ๏ธ 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__inner {/* ์ „์ฒด ์ด๋ฏธ์ง€๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ๋ถ€๋ชจ : ์›€์ง์ด๋Š” ๋ถ€๋ถ„ ์†์„ฑ์ฃผ๊ธฐ */
    display: flex;
    flex-wrap: wrap;
    width: 4800px; /* ์ด ์—ฌ์„ฏ๊ฐœ์˜ ์ด๋ฏธ์ง€ */
    height: 450px;
}
.slider {   /* ๊ฐœ๋ณ„์ ์ธ ์ด๋ฏธ์ง€ (๊ฐ๊ฐ์˜ ์ด๋ฏธ์ง€ ์„ค์ •) */
    position: relative;
    width: 800px;
    height: 450px;
}
.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: '์ด๋ฏธ์ง€6';}
.slider:nth-child(2)::before {content: '์ด๋ฏธ์ง€7';}
.slider:nth-child(3)::before {content: '์ด๋ฏธ์ง€8';}
.slider:nth-child(4)::before {content: '์ด๋ฏธ์ง€9';}
.slider:nth-child(5)::before {content: '์ด๋ฏธ์ง€10';}
.slider:nth-child(6)::before {content: '์ด๋ฏธ์ง€6';}

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

#2. JAVASCRIPT : ์Šคํฌ๋ฆฝํŠธ ์งœ๊ธฐ

๋ฉ”์„œ๋“œ์ธ setInterval()๊ณผ setTimeout() ๊ทธ๋ฆฌ๊ณ  ํฌ๊ธฐ ์†์„ฑ์ธ .offsetWidth์„ ํ†ตํ•ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์™„์„ฑํ•ด๋ณด์ž!

๐Ÿ’ก ์ „์ฒด ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ
const sliderWrap = document.querySelector(".slider__wrap");
const sliderImg = document.querySelector(".slider__img");   //๋ณด์—ฌ์ง€๋Š” ์˜์—ญ
const sliderInner = document.querySelector(".slider__inner"); //์›€์ง์ด๋Š” ์˜์—ญ
const slider = sliderWrap.querySelectorAll(".slider");      //๊ฐ๊ฐ์˜ ์ด๋ฏธ์ง€

let currentIndex = 0; //ํ˜„์žฌ ๋ณด์ด๋Š” ์ด๋ฏธ์ง€
let sliderCount = slider.length; //์ด๋ฏธ์ง€ ์ด ๊ฐฏ์ˆ˜
let sliderWidth = sliderImg.offsetWidth; //์ด๋ฏธ์ง€ ๊ฐ€๋กœ ๊ฐ’ (offsetWidth : ๊ฐ€๋กœ ๊ฐ’ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ) sliderImg:์š”์†Œ
let sliderClone = sliderInner.firstElementChild.cloneNode(true); // ์ œ์ด์ฟผ๋ฆฌ๋Š” ํด๋ก ๋งŒ ์ ๊ธฐ ๋ณต์‚ฌ๋ฅผ ์œ„ํ•ด์„œ๋Š” 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){
    //     sliderInner.style.transition = "0s";
    //     sliderInner.style.transform = "translateX(0px)";
    //     currentIndex = 0; 
    // ์ด ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ๋งˆ์ง€๋ง‰์—์„œ ์ฒซ๋ฒˆ์จฐ๋กœ ๋„˜์–ด๊ฐˆ ๋•Œ ์–ด์ƒ‰ํ•˜๊ฒŒ ๋„˜์–ด๊ฐ€์ง„๋‹ค. (์• ๋‹ˆ๋ฉ”์ด์…˜ ์ ์šฉ์ด ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ.)
    // }

    //๋งˆ์ง€๋ง‰ ์‚ฌ์ง„์— ์œ„์น˜ํ–ˆ์„ ๋•Œ : ํšจ์œจ์„ฑ๋ณด๋‹จ ๊ฐ„ํŽธํ•œ ์ฝ”๋“œ 
    //์ฒซ๋ฒˆ์งธ๋กœ ๋„˜์–ด๊ฐˆ ๋•Œ ์Šค๋ฌด์Šคํ•˜๊ฒŒ ๋„˜์–ด๊ฐ€๋„๋ก ํ•œ ๊ฒƒ.
    if(currentIndex == sliderCount){
        setTimeout(() => {
            sliderInner.style.transition = "0s";
            sliderInner.style.transform = "translateX(0px)";
        },700);

        currentIndex = 0; //๋งˆ์ง€๋ง‰ ์‚ฌ์ง„์ด ์™”์„ ๋•Œ ๋‹ค์Œ ์ด๋ฏธ์ง€๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š๊ณ  ๋‹ค์‹œ ์ดˆ๊ธฐํ™” ์‹œํ‚ค๊ธฐ
    }

}
setInterval(sliderEffect,2000);
//setInterval : ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐ˜๋ณต์‹œ์ผœ ์ฃผ๋Š” ๊ฒƒ
//setTimeout : ํ•œ ๋ฒˆ๋งŒ ์‚ฌ์šฉ

โœ๏ธ ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

! JAVASCRIPT TIP !
1. ์Šคํฌ๋ฆฝํŠธ์— ํ•„์š”ํ•œ ์„ ํƒ์ž๋ฅผ ์„ค์ •ํ•ด์ฃผ๊ณ  ํ˜„์žฌ ๋ณด์—ฌ์•ผํ•  ์ด๋ฏธ์ง€์—๊ฒŒ 0์„ ๋ถ€์—ฌํ•˜์—ฌ ์ฒซ ์ˆœ์„œ๊ฐ€ ๋˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
2. ๋‹ค์Œ ์ด๋ฏธ์ง€๊ฐ€ ๋ณด์ด๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ฏธ์ง€ ์ด ๊ฐฏ์ˆ˜ / ์ด๋ฏธ์ง€์˜ ๊ฐ€๋กœ๊ฐ’์„ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜์—ฌ ์ด๋ฏธ์ง€ ๊ฐ€๋กœ * ๋ณด์—ฌ์•ผํ•  ์ด๋ฏธ์ง€๊ฐ€ ๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
3. ์ฒซ๋ฒˆ์งธ ์ž์‹์š”์†Œ๋ฅผ ๋ณต์‚ฌํ•ด์ฃผ๋Š” ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ 6๋ฒˆ์งธ ์ด๋ฏธ์ง€๊นŒ์ง€ ๋ณด์ด๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
4. ๊ทธ ์™ธ์—๋Š” ๋ฐ์— ์ฃผ์„์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋” ์ž์„ธํ•˜๊ฒŒ ์•Œ์•„๋ด…์‹œ๋‹ค!

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

let currentIndex = 0; //ํ˜„์žฌ ๋ณด์ด๋Š” ์ด๋ฏธ์ง€
let sliderCount = slider.length; //์ด๋ฏธ์ง€ ์ด ๊ฐฏ์ˆ˜
let sliderWidth = sliderImg.offsetWidth; //์ด๋ฏธ์ง€ ๊ฐ€๋กœ ๊ฐ’ (offsetWidth : ๊ฐ€๋กœ ๊ฐ’ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ) sliderImg:์š”์†Œ
let sliderClone = sliderInner.firstElementChild.cloneNode(true); // ์ œ์ด์ฟผ๋ฆฌ๋Š” ํด๋ก ๋งŒ ์ ๊ธฐ ๋ณต์‚ฌ๋ฅผ ์œ„ํ•ด์„œ๋Š” true ๊ผญ ์ ๊ธฐ : ์ฒซ๋ฒˆ์งธ ์ž์‹ ์š”์†Œ ๋ณต์‚ฌํ•˜๊ธฐ(์ด๋ฏธ์ง€)

sliderInner.appendChild(sliderClone);   //์ฒซ๋ฒˆ์งธ ์ด๋ฏธ์ง€๋ฅผ ๋งˆ์ง€๋ง‰์— ๋„ฃ์–ด์ค€๋‹ค.

function sliderEffect(){
    currentIndex++;
    sliderInner.style.transition = "all 0.6s";

    //x์ถ•์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ sliderWidth * currentIndex ๋งŒํผ ์ด๋™์‹œ์ผœ์„œ ๋ณด์ด๊ฒŒ ํ•ด๋ผ : ์ธ๋ฑ์Šค๊ฐ€ ๊ณฑํ•ด์ง€๋ฉด ์ด๋ฏธ์ง€ ์˜์—ญ์—์„œ ๊ณฑํ•ด์ง„ ๋งŒํผ์˜ px์„ ์ด๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ์ด๋ฏธ์ง€๊ฐ€ ๋ณด์ด๊ฒŒ ๋˜๋Š” ๊ฒƒ
    sliderInner.style.transform = "translateX(-" + sliderWidth * currentIndex + "px)";

    //์ฒซ๋ฒˆ์งธ๋กœ ๋„˜์–ด๊ฐˆ ๋•Œ ์–ด์ƒ‰ํ•˜๊ฒŒ ๋„˜์–ด๊ฐ€์ง€ ์•Š๋„๋ก setTimeout์„ ํ†ตํ•ด ๋ณต์‚ฌํ•œ ์ฒซ๋ฒˆ์งธ ์ด๋ฏธ์ง€(6๋ฒˆ์งธ ์ด๋ฏธ์ง€)์— ์†์„ฑ ๋ถ€์—ฌ
    if(currentIndex == sliderCount){
        setTimeout(() => {
            sliderInner.style.transition = "0s";
            sliderInner.style.transform = "translateX(0px)";
        },700); //0.7์ดˆ์— ์ด๋ฏธ์ง€ ๋„˜์–ด๊ฐ€๋„๋ก 

        currentIndex = 0; //๋งˆ์ง€๋ง‰ ์‚ฌ์ง„์ด ์™”์„ ๋•Œ ๋‹ค์Œ ์ด๋ฏธ์ง€๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š๊ณ  ๋‹ค์‹œ ์ดˆ๊ธฐํ™” ์‹œํ‚ค๊ธฐ
    }
}
setInterval(sliderEffect,2000); //2์ดˆ์— ์ด๋ฏธ์ง€๊ฐ€ ๋„˜์–ด๊ฐ€๋„๋ก

#3. ๊ฒฐ๊ณผ ๋ณด๊ธฐ ๐ŸŽ‰

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css