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

์Šฌ๋ผ์ด๋“œ ํšจ๊ณผ - ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋“œ(๋ฒ„ํŠผ, dot๋ฉ”๋‰ด)

by _ํ† ๋งคํ†  2022. 10. 24.
728x90

 

์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ - ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋“œ(๋ฒ„ํŠผ, dot๋ฉ”๋‰ด)

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์ž๋™์œผ๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š๊ณ  ๋ฒ„ํŠผ๊ณผ ๋‹ท ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ–ˆ์„ ๋•Œ ํ•ด๋‹น ์ˆœ์„œ๋กœ ์ด๋ฏธ์ง€๊ฐ€ ๋„˜์–ด๊ฐ€๋„๋ก ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ๐Ÿฅน


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

! HTML TIP !
๊ฐ ์ด๋ฏธ์ง€๋งˆ๋‹ค css์„ค์ •์„ ์ฃผ๊ธฐ ์œ„ํ•ด ๋”ฐ๋กœ ๋ถ€๋ชจ๋ฅผ 2๋ฒˆ ๊ฐ์‹ธ์„œ ๋ฌถ์–ด์ค๋‹ˆ๋‹ค.
๋˜ํ•œ ๋ฒ„ํŠผ ์•„๋ž˜ ๋‹ท๋ฉ”๋‰ด ๊ตฌ์กฐ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ท ๋ฉ”๋‰ด๋Š” ์Šคํฌ๋ฆฝํŠธ๋กœ ๊ตฌํ˜„ํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์ฃผ์„์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค.

โœ๏ธ HTML ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ
<section id="sliderType05">
    <div class="slider__wrap">
        <div class="slider__img">
            <div class="slider__inner">
                <div class="slider" role="group" aria-label="1/5">
                    <img src="../assets/img/Mountian_bg01.jpg" alt="์ด๋ฏธ์ง€6">
                </div>
                <div class="slider" role="group" aria-label="2/5">
                    <img src="../assets/img/Mountian_bg02.jpg" alt="์ด๋ฏธ์ง€7">
                </div>
                <div class="slider" role="group" aria-label="3/5">
                    <img src="../assets/img/Mountian_bg03.jpg" alt="์ด๋ฏธ์ง€8">
                </div>
                <div class="slider" role="group" aria-label="4/5">
                    <img src="../assets/img/Mountian_bg04.jpg" alt="์ด๋ฏธ์ง€9">
                </div>
                <div class="slider" role="group" aria-label="5/5">
                    <img src="../assets/img/Mountian_bg05.jpg" alt="์ด๋ฏธ์ง€10">
                </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>
        <div class="slider__dot">
            <!-- <a href="#" class="dot active">์ด๋ฏธ์ง€1</a>
            <a href="#" class="dot">์ด๋ฏธ์ง€2</a>
            <a href="#" class="dot">์ด๋ฏธ์ง€3</a>
            <a href="#" class="dot">์ด๋ฏธ์ง€4</a>
            <a href="#" class="dot">์ด๋ฏธ์ง€5</a> -->
        </div>
    </div>
</section>

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

โœ๏ธ 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;
    }
    .slider {
        width: 400px;
        height: 250px;
    }
}

.slider__btn a {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 50px;
    height: 50px;
    background: rgba(0,0,0,0.4);
    text-align: center;
    line-height: 50px;
    transition: all 0.2s;
    display: block;
    color: #fff;
}
.slider__btn a:hover {
    border-radius: 50px;
    background: rgba(38, 52, 103, 0.4);
}
.slider__btn a.prev {
    left: 0;
}
.slider__btn a.next {
    right: 0;
}
.slider__dot {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: 20px;
}
.slider__dot .dot {
    width: 20px;
    height: 20px;
    background: rgba(0,0,0,0.4);
    display: inline-block;
    border-radius: 50%;
    text-indent: -9999px;
    transition: all 0.3s;
    margin: 2px;
}
.slider__dot .dot.active {
    background: rgba(255,255,255,0.9);
}

#2. 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= sliderBtn.querySelector(".prev"); // ์™ผ์ชฝ๋ฒ„ํŠผ
    const sliderBtnNext = sliderBtn.querySelector(".next"); // ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ
    const sliderDot = document.querySelector(".slider__dot"); // ๋‹ท๋ฉ”๋‰ด

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

    let dotIndex = "";
    
    function intit(){
        // dot ๋งŒ๋“ค์–ด์ฃผ๊ธฐ!
        // slider.forEach(() => {
        //     dotIndex += "<a href='#' class='dot'>์ด๋ฏธ์ง€1</a>";
        // });
        // dot ๋งŒ๋“ค์–ด์ฃผ๊ธฐ!
        slider.forEach(() => dotIndex += "<a href='#' class='dot'>์ด๋ฏธ์ง€1</a>");
        sliderDot.innerHTML = dotIndex;

        //์ฒซ๋ฒˆ์งธ ๋‹ท ๋ฒ„ํŠผ ํ™œ์„ฑํ™” ํ‘œ์‹œ(active)
        sliderDot.firstChild.classList.add("active");

    }
    intit();

    //์ด๋ฏธ์ง€ ์ด๋™ ์Šคํฌ๋ฆฝํŠธ
    function gotoSlider(num){
        sliderInner.style.transition = "all 400ms";
        sliderInner.style.transform = "translateX(" + -sliderWidth * num + "px)";
        currentIndex = num;


        //๋‘๋ฒˆ์žฌ ์ด๋ฏธ์ง€์—์„œ ๋‘๋ฒˆ์งธ ๋‹ท ๋ฒ„ํŠผ ํ™œ์„ฑํ™” (ํƒญ ๋ฉ”๋‰ด์™€ ์›๋ฆฌ ๋™์ผ)
        //1. ๋‹ท ๋ฉ”๋‰ด์˜ ํด๋ž˜์Šค๋ฅผ ๋ชจ๋‘ ์‚ญ์ œ
        //2. ํ•ด๋‹น ์ด๋ฏธ์ง€์˜ ํ•ด๋‹น ๋‹ท ๋ฉ”๋‰ด์— ํด๋ž˜์Šค ์ถ”๊ฐ€
        let dotActive = document.querySelectorAll(".slider__dot .dot");
        dotActive.forEach(el => el.classList.remove("active"));
        dotActive[num].classList.add("active");
    }

    //๋ฒ„ํŠผ์„ ํด๋ฆญํ•  ๋•Œ ์ด๋ฏธ์ง€ ์ด๋™ ์ž‘๋™์‹œํ‚ค๊ธฐ
    document.querySelectorAll(".slider__btn a").forEach((btn, index) => {
        btn.addEventListener("click", () => {
            let prevIndex = (currentIndex + (sliderCount - 1 )) % sliderCount;
            let nextIndex = (currentIndex + 1) % sliderCount;

            if(btn.classList.contains("prev")){
                gotoSlider(prevIndex);
            }else{
                gotoSlider(nextIndex);
            }
        });
    });

    //dot ๋ฉ”๋‰ด๋ฅผ ํด๋ฆญํ•  ๋•Œ ์ด๋ฏธ์ง€ ์ด๋™ ์ž‘๋™์‹œํ‚ค๊ธฐ
    document.querySelectorAll(".slider__dot .dot").forEach((dot, index) => {
        dot.addEventListener("click", () => {
            gotoSlider(index);
        });
    });

    //์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์ˆœ์„œ๋Œœ๋กœ ์ง„ํ–‰๋˜๋ฏ€๋กœ ์‹คํ–‰๋ฌธ์ด ๋งจ ๋ฐ‘์— ์žˆ์–ด์„œ ๋‚˜์˜ค์ง€ ์•Š์€ ๊ฒƒ.
}

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

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css