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

[EFFECT] ์Šฌ๋ผ์ด๋“œ ํšจ๊ณผ 04 : ๋ฒ„ํŠผ์œผ๋กœ ์ด๋ฏธ์ง€ ์ด๋™์‹œํ‚ค๊ธฐ!

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

์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋“œ : ๋ฒ„ํŠผ์„ ํ†ตํ•ด ์Šฌ๋ผ์ด๋“œ ์›€์ง์ด๊ธฐ (์™ผ์ชฝ ์˜ค๋ฅธ์ชฝ ๋ชจ๋‘ ๋™์ž‘)

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


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

! HTML TIP !
๊ฐ ์ด๋ฏธ์ง€๋งˆ๋‹ค css์„ค์ •์„ ์ฃผ๊ธฐ ์œ„ํ•ด ๋”ฐ๋กœ ๋ถ€๋ชจ๋ฅผ 2๋ฒˆ ๊ฐ์‹ธ์„œ ๋ฌถ์–ด์ค๋‹ˆ๋‹ค.
๊ตฌ์กฐ๋Š” ์ง€๋‚œ ์‹œ๊ฐ„๊ณผ ๋˜‘๊ฐ™์œผ๋‚˜ slider__img ๋ฐ‘์— ํ˜•์ œ ์š”์‡ผ๋กœ ๋ฒ„ํŠผ ์˜์—ญ์„ ์„ ์–ธํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค!

โœ๏ธ HTML ์Šคํฌ๋ฆฝํŠธ ๋ณด๊ธฐ
<section id="sliderType04">
    <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>
</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;
}

#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= document.querySelector(".prev"); // ์™ผ์ชฝ๋ฒ„ํŠผ
const sliderBtnNext = document.querySelector(".next"); // ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ

let currentIndex = 0; //ํ˜„์žฌ ๋ณด์ด๋Š” ์ด๋ฏธ์ง€ : ๋งˆํฌ์—… ์ˆœ์„œ๋Œ€๋กœ 0๋ฒˆ๋ถ€ํ„ฐ 4๋ฒˆ
let sliderCount = slider.length;    //์ด๋ฏธ์ง€ ๊ฐฏ์ˆ˜
let sliderWidth = sliderImg.offsetWidth; //์ด๋ฏธ์ง€ ๊ฐ€๋กœ๊ฐ’

//์ด๋ฏธ์ง€ ์›€์ง์ด๋Š” ์˜์—ญ
//gotoSlider ํ•จ์ˆ˜์— num๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๊ณ  sliderInner ์„ ํƒ์ž์— ์—๋‹ˆ๋ฉ”์ด์…˜ ์†์„ฑ์„ ๋ถ€์—ฌํ•œ๋‹ค.
//translateX X์ถ•์œผ๋กœ -sliderWidth * num ๋งŒํผ ์˜์—ญ์„ ์ด๋™ํ•˜๋„๋ก ํ•œ๋‹ค. : ์—ฌ๊ธฐ์„œ -๋ฅผ ๋ถ™์€ ์ด์œ ๋Š” ์™ผ์ชฝ์œผ๋กœ ์›€์ง์ด๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•จ!
//์™œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์“ฐ๋Š” ๊ฐ€? : ์ด๋ฏธ์ง€์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•จ. (์•„๋ž˜์˜ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์—์„œ ์ธ๋ฑ์Šค ๊ฐ’์„ ์ถ”์ถœ)
function gotoSlider(num){
    sliderInner.style.transition = "all 400ms";
    sliderInner.style.transform = "translateX("+ -sliderWidth * num +"px)";
    currentIndex = num; // ๋ณด์ด๋Š” ์ด๋ฏธ์ง€๊ฐ€ ๋ฐ”๋€Œ๋„๋ก ์„ค์ •
}

//์™„์ชฝ ๋ฒ„ํŠผ ํด๋ฆญ
//43210
//sliderCount(์ด๋ฏธ์ง€ ๊ฐฏ์ˆ˜ : 6๊ฐœ) + currentIndex(ํ˜„์žฌ ์ด๋ฏธ์ง€์˜ ์ˆœ์„œ : 4) -1 == 9
// 9 % 5 == ๋‚˜๋จธ์ง€ ๊ฐ’์€ 4์ด๋ฏ€๋กœ 4๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋Œ์•„๊ฐ„๋‹ค.
sliderBtnPrev.addEventListener("click", () => {
    let prevIndex = (sliderCount + currentIndex -1) % sliderCount; //0๋ฒˆ์ด ๋˜์—ˆ์„ ๋•Œ 5%5 ==0์ด ๋˜๋ฏ€๋กœ ๊ณ„์† ํ•ด์„œ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ.
    gotoSlider(prevIndex);
});

//์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ ํด๋ฆญ
//01234
sliderBtnNext.addEventListener("click", () => {
    let nextIndex = (currentIndex +1) % sliderCount; //5 % 5์˜ ๋‚˜๋จธ์ง€ ๊ฐ’์ด 0์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ 0๋ฒˆ์งธ ์ˆœ์„œ๋กœ ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ.
    gotoSlider(nextIndex);
});

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

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


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

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css