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

์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ 4. ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ ๋„˜๊ธฐ๊ธฐ

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

Javascript Slider Effect

Slider ์ดํŽ™ํŠธ ์ค‘ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ด๋ฏธ์ง€๋ฅผ ์˜†์œผ๋กœ ๋„˜๊ธฐ๋Š” ํšจ๊ณผ์ž…๋‹ˆ๋‹ค.

HTML

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

<main id="main">
    <section id="sliderType02">
        <div class="slider__wrap">
            <div class="slider__img">
                <div class="slider__inner">
                    <div class="slider" role="group" aria-label="1/5"><img src="../effect/img/effect_sub01-min.jpg" alt="์Šฌ๋ผ์ด๋“œ ์ด๋ฏธ์ง€1"></div>
                    <div class="slider" role="group" aria-label="2/5"><img src="../effect/img/effect_sub02-min.jpg" alt="์Šฌ๋ผ์ด๋“œ ์ด๋ฏธ์ง€2"></div>
                    <div class="slider" role="group" aria-label="3/5"><img src="../effect/img/effect_sub03-min.jpg" alt="์Šฌ๋ผ์ด๋“œ ์ด๋ฏธ์ง€3"></div>
                    <div class="slider" role="group" aria-label="4/5"><img src="../effect/img/effect_sub04-min.jpg" alt="์Šฌ๋ผ์ด๋“œ ์ด๋ฏธ์ง€4"></div>
                    <div class="slider" role="group" aria-label="5/5"><img src="../effect/img/effect_sub05-min.jpg" alt="์Šฌ๋ผ์ด๋“œ ์ด๋ฏธ์ง€5"></div>
                </div>
            </div>
        </div>
        <div class="slider__btn">
            <a href="#" class="prev" role="button" aria-label="์™ผ์ชฝ ์ด๋ฏธ์ง€">prev</a>
            <a href="#" class="next" role="button" aria-label="์˜ค๋ฅธ์ชฝ ์ด๋ฏธ์ง€">next</a>
        </div>
    </section>
</main>

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

/* slide btn */

.slider__btn a{
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 50px;
    height: 50px;
    background: rgba(0,0,0,0.3);
    text-align: center;
    line-height: 45px;
    transition: all 0.2s;
    display: block;
    color: #fff;
}

.slider__btn a:hover {
    background: rgb(255, 71, 71);
    border-radius: 40%;
}
.slider__btn a.prev {
    left: 0;
}
.slider__btn a.next {
    right: 0;
}

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");                //๊ฐ๊ฐ์˜ ์ด๋ฏธ์ง€

const sliderBtn = document.querySelector(".slider__btn");        //๋ฒ„ํŠผ
const sliderBtnPrev = document.querySelector(".prev");           //์™ผ์ชฝ๋ฒ„ํŠผ
const sliderBtnNext = document.querySelector(".next");           //์˜ค๋ฅธ์ชฝ๋ฒ„ํŠผ

let currentIndex = 0;                   //ํ˜„์žฌ ์ด๋ฏธ์ง€
let sliderCount = slider.length;        //์ด๋ฏธ์ง€ ๊ฐฏ์ˆ˜
let sliderWidth = sliderImg.offsetWidth //์ด๋ฏธ์ง€ ๊ฐ€๋กœ๊ฐ’

//์ด๋ฏธ์ง€ ์›€์ง์ด๋Š” ์˜์—ญ
function gotoSlider (num) {
    sliderInner.style.transition = "all 400ms"
    sliderInner.style.transform = "translateX(" + -sliderWidth * num + "px)";
    currentIndex = num;
};

//์™ผ์ชฝ ๋ฒ„ํŠผ ํด๋ฆญ(์ฃผ๋ง์ˆ™์ œ)
sliderBtnPrev.addEventListener("click", ()=>{
    let prevIndex = (currentIndex + (sliderCount-1)) % sliderCount
    //4 3 2 1 0
    gotoSlider(prevIndex);
});

//์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ ํด๋ฆญ
sliderBtnNext.addEventListener("click", ()=>{
    let nextIndex = (currentIndex + 1) % sliderCount
    gotoSlider(nextIndex);
    // 0 1 2 3 4
});
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€