๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Effect/Slide Effect

[Slide Effect] ์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ 01

by ์ฝ”๋”ฉ๊ณต์ฑ… 2022. 8. 25.
๋ฐ˜์‘ํ˜•

์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ 01

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๊ทธ ๋™์•ˆ ๋ฐฐ์› ๋˜ JavaScript๋ฅผ ์ด์šฉํ•˜์—ฌ ์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. HTML๊ณผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค๋ฅผ ์„ค๋ช…๋“œ๋ฆฌ๋ฉฐ CSS๋Š” ๋ณต์žกํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์— CSS ์†Œ์Šค ๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ๋ด ์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

์ž, ๊ทธ๋Ÿผ ์‹œ์ž‘ํ•ด๋ณผ๊นŒ์š”?


HTML ์†Œ์Šค ๋ณด๊ธฐ : ๋ชจ๋‹ฌ ๋ฐ•์Šค

์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ 01์˜ HTML ์†Œ์Šค์™€ ๋ถ€์—ฐ ์„ค๋ช…(์ฃผ์„ ์ฒ˜๋ฆฌ๋จ)์ด๋‹ค.

<body class="img06"> <!-- ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ด๋ฏธ์ง€ / CSS๋กœ ์ด๋ฏธ์ง€ ์ถœ๋ ฅ์‹œํ‚ด -->
    <header id="header">
        <h1>JAVASCRIPT SLIDER EFFECT 01</h1>
        <p>์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ โ‘  - ํŠธ๋žœ์ง€์…˜ ํšจ๊ณผ</p>
        <ul>
            <li class="active"><a href="sliderEffect01.html">01</a></li>
            <li><a href="sliderEffect02.html">02</a></li>
            <li><a href="sliderEffect03.html">03</a></li>
            <li><a href="sliderEffect04.html">04</a></li>
            <li><a href="sliderEffect05.html">05</a></li>
            <li><a href="sliderEffect06.html">06</a></li>
            <li><a href="sliderEffect07.html">07</a></li>
        </ul>
    </header>
    <!-- // header -->

    <main id="main">
        <section id="sliderType01">
            <div class="slider__wrap">
                <div class="slider__img">
                    <div class="slider"><img src="../assets/img/effect_bg03.jpg" alt="์ด๋ฏธ์ง€1"></div>
                    <div class="slider"><img src="../assets/img/effect_bg04.jpg" alt="์ด๋ฏธ์ง€2"></div>
                    <div class="slider"><img src="../assets/img/effect_bg10.jpg" alt="์ด๋ฏธ์ง€3"></div>
                    <div class="slider"><img src="../assets/img/effect_bg07.jpg" alt="์ด๋ฏธ์ง€4"></div>
                    <div class="slider"><img src="../assets/img/effect_bg08.jpg" alt="์ด๋ฏธ์ง€5"></div>
                </div>
            </div>
        </section>
    </main>
    <!-- // main -->

    <footer id="footer">
        <div class="modal__wrap">
            <div class="modal__btn">
                <label for="btn">์†Œ์Šค ๋ณด๊ธฐ</label>
            </div>
            <div class="modal__cont">
                <div class="modal__box">
                    <div class="title">
                        <span class="dot" aria-label="true"></span>
                        <div class="tabs">
                            <div class="active">
                                <span class="favicon">
                                    <svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true" focusable="false">
                                        <circle cx="9" cy="9" r="9" fill="#fff"></circle>
                                        <path d="M18 9C18 13.9706 15.4688 3.09375 9 10.125C2.53125 17.1562 0 13.9706 0 9C0 4.02944 4.02944 0 9 0C13.9706 0 18 4.02944 18 9Z" fill="orange"></path>
                                        <path d="M18 9C18 13.9706 14.9062 0.84375 8.29688 6.1875C1.6875 11.5312 0 13.9706 0 9C0 4.02944 4.02944 0 9 0C13.9706 0 18 4.02944 18 9Z" fill="yellow"></path>
                                    </svg>
                                </span>
                                <en>JavaScript</en>
                                <span class="close">
                                    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" focusable="false">
                                        <path d="M12.5 3.5L3.5 12.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                                        <path d="M12.5 12.5L3.5 3.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                                    </svg>
                                </span>
                            </div>
                            <div>
                                <span class="favicon">
                                    <svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true" focusable="false">
                                        <circle cx="9" cy="9" r="9" fill="#fff"></circle>
                                        <path d="M18 9C18 13.9706 15.4688 3.09375 9 10.125C2.53125 17.1562 0 13.9706 0 9C0 4.02944 4.02944 0 9 0C13.9706 0 18 4.02944 18 9Z" fill="orange"></path>
                                        <path d="M18 9C18 13.9706 14.9062 0.84375 8.29688 6.1875C1.6875 11.5312 0 13.9706 0 9C0 4.02944 4.02944 0 9 0C13.9706 0 18 4.02944 18 9Z" fill="yellow"></path>
                                    </svg>
                                </span>
                                <en>HTML</en>
                                <span class="close">
                                    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" focusable="false">
                                        <path d="M12.5 3.5L3.5 12.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                                        <path d="M12.5 12.5L3.5 3.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                                    </svg>
                                </span>
                            </div>
                            <div>
                                <span class="favicon">
                                    <svg width="16" height="16" viewBox="0 0 18 18" fill="none" aria-hidden="true" focusable="false">
                                        <circle cx="9" cy="9" r="9" fill="#fff"></circle>
                                        <path d="M18 9C18 13.9706 15.4688 3.09375 9 10.125C2.53125 17.1562 0 13.9706 0 9C0 4.02944 4.02944 0 9 0C13.9706 0 18 4.02944 18 9Z" fill="orange"></path>
                                        <path d="M18 9C18 13.9706 14.9062 0.84375 8.29688 6.1875C1.6875 11.5312 0 13.9706 0 9C0 4.02944 4.02944 0 9 0C13.9706 0 18 4.02944 18 9Z" fill="yellow"></path>
                                    </svg>
                                </span>
                                <en>CSS</en>
                                <span class="close">
                                    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" focusable="false">
                                        <path d="M12.5 3.5L3.5 12.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                                        <path d="M12.5 12.5L3.5 3.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                                    </svg>
                                </span>
                            </div>
                        </div>
                        <span class="plus" aria-label="true">
                            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" focusable="false">
                                <path d="M2.5 8H13.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                                <path d="M8 2.5V13.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                            </svg>
                        </span>
                    </div>
                    <div class="cont">
                        <div class="active">
<pre><code lang="language-js">// ์„ ํƒ์ž
const searchKeyword = document.querySelectorAll(".search__info .keyword span");   // ์•ŒํŒŒ๋ฒณ
const searchList = document.querySelectorAll(".search__list ul li");    // ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ
const searchInfo = document.querySelector(".search__info .num");        // ์ „์ฒด ๊ฐœ์ˆ˜
let count = 0;

// ์ „์ฒด ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
searchInfo.textContent = searchList.length; // CSS ์†์„ฑ ์ด ๊ฐœ์ˆ˜ ์ถœ๋ ฅ
</code></pre>
                        </div>
                        <div>
<pre><code lang="language-js">// ์„ ํƒ์ž2
const searchKeyword = document.querySelectorAll(".search__info .keyword span");   // ์•ŒํŒŒ๋ฒณ
const searchList = document.querySelectorAll(".search__list ul li");    // ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ
const searchInfo = document.querySelector(".search__info .num");        // ์ „์ฒด ๊ฐœ์ˆ˜
let count = 0;

// ์ „์ฒด ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
searchInfo.textContent = searchList.length; // CSS ์†์„ฑ ์ด ๊ฐœ์ˆ˜ ์ถœ๋ ฅ

// ์„ ํƒ์ž2
const searchKeyword = document.querySelectorAll(".search__info .keyword span");   // ์•ŒํŒŒ๋ฒณ
const searchList = document.querySelectorAll(".search__list ul li");    // ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ
const searchInfo = document.querySelector(".search__info .num");        // ์ „์ฒด ๊ฐœ์ˆ˜
let count = 0;

// ์ „์ฒด ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
searchInfo.textContent = searchList.length; // CSS ์†์„ฑ ์ด ๊ฐœ์ˆ˜ ์ถœ๋ ฅ ffffffffffffffffffffffffffffffffffffffffffffffffffffffff
</code></pre>
                        </div>
                        <div>
<pre><code lang="language-js">// ์„ ํƒ์ž3
const searchKeyword = document.querySelectorAll(".search__info .keyword span");   // ์•ŒํŒŒ๋ฒณ
const searchList = document.querySelectorAll(".search__list ul li");    // ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ
const searchInfo = document.querySelector(".search__info .num");        // ์ „์ฒด ๊ฐœ์ˆ˜
let count = 0;

// ์ „์ฒด ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
searchInfo.textContent = searchList.length; // CSS ์†์„ฑ ์ด ๊ฐœ์ˆ˜ ์ถœ๋ ฅdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd

// ์„ ํƒ์ž3
const searchKeyword = document.querySelectorAll(".search__info .keyword span");   // ์•ŒํŒŒ๋ฒณ
const searchList = document.querySelectorAll(".search__list ul li");    // ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ
const searchInfo = document.querySelector(".search__info .num");        // ์ „์ฒด ๊ฐœ์ˆ˜
let count = 0;

// ์ „์ฒด ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
searchInfo.textContent = searchList.length; // CSS ์†์„ฑ ์ด ๊ฐœ์ˆ˜ ์ถœ๋ ฅ

// ์„ ํƒ์ž3
const searchKeyword = document.querySelectorAll(".search__info .keyword span");   // ์•ŒํŒŒ๋ฒณ
const searchList = document.querySelectorAll(".search__list ul li");    // ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ
const searchInfo = document.querySelector(".search__info .num");        // ์ „์ฒด ๊ฐœ์ˆ˜
let count = 0;

// ์ „์ฒด ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
searchInfo.textContent = searchList.length; // CSS ์†์„ฑ ์ด ๊ฐœ์ˆ˜ ์ถœ๋ ฅ yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
</code></pre>
                        </div>
                    </div>
                </div>
                <div class="modal__close">
                    <span class="close">
                        <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" focusable="false">
                            <path d="M12.5 3.5L3.5 12.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                            <path d="M12.5 12.5L3.5 3.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
                        </svg>
                    </span>
                </div>
            </div>
        </div>
    </footer>
    <!-- // footer -->
</body>

JavaScript ์†Œ์Šค ๋ณด๊ธฐ : ๋ชจ๋‹ฌ ๋ฐ•์Šค

์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ 01์˜ JavaScript ์†Œ์Šค์™€ ๋ถ€์—ฐ ์„ค๋ช…(์ฃผ์„ ์ฒ˜๋ฆฌ๋จ)์ด๋‹ค.
์ด ๋‚ด์šฉ์€ ๋”ฐ๋กœ common.min.js ํŒŒ์ผ๋กœ ๋นผ๋†“๋„๋ก ํ•˜์ž.

hljs.highlightAll();

// ๋ชจ๋‹ฌ ์ฐฝ
const modalBtn = document.querySelector(".modal__btn");     // ์†Œ์Šค ๋ณด๊ธฐ ๋ฒ„ํŠผ
const modalCont = document.querySelector(".modal__cont");   // ๋ชจ๋‹ฌ ์ฐฝ
const modalClose = document.querySelector(".modal__close"); // ๋ชจ๋‹ฌ ์ฐฝ ๋‹ซ๊ธฐ ๋ฒ„ํŠผ

modalBtn.addEventListener("click", () => {
    modalCont.classList.add("show");    // ์†Œ์Šค๋ณด๊ธฐ ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ๋ชจ๋‹ฌ ์ฐฝ ๋„์šฐ๊ธฐ
    modalCont.classList.remove("hide");
});

modalClose.addEventListener("click", () => {
    modalCont.classList.add("hide");     // X ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ๋ชจ๋‹ฌ ์ฐฝ ๋‹ซ๊ธฐ
});

// ํƒญ ๋ฉ”๋‰ด
const tabBtn = document.querySelectorAll(".modal__box .tabs > div");
const tabCont = document.querySelectorAll(".modal__box .cont > div");

tabBtn.forEach((element, index) => {
    // ๋ฒ„ํŠผ์„ ํด๋ฆญํ•จ
    element.addEventListener("click", (event) => {
        event.preventDefault();   // ํด๋ฆญํ•œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฆ„์œผ๋กœ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ ๋ฌดํšจํ™”
        // ๋ฒ„ํŠผ ํด๋ฆญํ•ด๋„(href="#" ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•˜๋Š”)๋Œ์˜ฌ ๋ง‰์„ ์ˆ˜ ์žˆ์Œ.
        
        // ํด๋ž˜์Šค active๋ฅผ ๋ชจ๋‘ ์ œ๊ฑฐ
        tabBtn.forEach(li => {
            li.classList.remove("active");      // li(๋ฒ„ํŠผ)์„ ๋ˆ„๋ฅด๋ฉด ๋จผ์ € active(๋ฒ„ํŠผ๋ˆŒ๋ฆฐ)ํšจ๊ณผ ์ง€์›Œ์ค€๋‹ค.
        });

        // ๋‚ด๊ฐ€ ํด๋ฆญํ•œ ๋ฒ„ํŠผ์— active ํšจ๊ณผ ์ถ”๊ฐ€
        element.classList.add("active");        // ๊ทธ๋Ÿฐ ๋‹ค์Œ ๋‚ด๊ฐ€ ๋ˆ„๋ฅธ ๋ฒ„ํŠผ์—๋งŒ active ํšจ๊ณผ๋ฅผ ๋„ฃ์–ด์ฃผ๋„๋ก ํ•œ๋‹ค.

        // ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋ชจ๋“  ์ž์‹ ๋ฐ•์Šค(์ฝ˜ํ…์ธ ) ์ˆจ๊ธฐ๊ธฐ
        tabCont.forEach(div => {
            div.style.display = "none";
        });

        // [i]ํด๋ฆญํ•œ ๋ฒ„ํŠผ๊ณผ [i]๊ทธ์— ์•Œ๋งž๋Š” ์ž์‹ ๋ฐ•์Šค(์ฝ˜ํ…์ธ )๋ฅผ ์ถœ๋ ฅ์‹œํ‚ด
        tabCont[index].style.display = "block";
    });
});

JavaScript ์†Œ์Šค ๋ณด๊ธฐ : ์ด๋ฏธ์ง€ ๋ณ€ํ™”

์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ 01์˜ 3์ดˆ๋งˆ๋‹ค ๋ณ€ํ™”ํ•˜๋Š” ์ด๋ฏธ์ง€ ํšจ๊ณผ JavaScript ์†Œ์Šค์™€ ๋ถ€์—ฐ ์„ค๋ช…(์ฃผ์„ ์ฒ˜๋ฆฌ๋จ)์ด๋‹ค.

// ์ด๋ฏธ์ง€ ๋ณ€ํ™”
<script src="../assets/js/highlight.min.js"></script>
<script src="../assets/js/common.min.js"></script>
<script>
    const sliderWrap = document.querySelector(".slider__wrap");
    const sliderImg = sliderWrap.querySelector(".slider__img");
    const slider = sliderWrap.querySelectorAll(".slider");

    let currentIndex = 0;       // ํ˜„์žฌ ๋ณด์ด๋Š” ์ด๋ฏธ์ง€
    let slideCount = slider.length      // ์ด๋ฏธ์ง€ ๊ฐœ์ˆ˜

    setInterval(() => {
        let nextIndex = (currentIndex + 1) % 5;  // ๋‹ค์Œ ์ด๋ฏธ์ง€

        slider[currentIndex].style.opacity = "0";  // ์ฒซ๋ฒˆ์งธ ์ด๋ฏธ์ง€๋ฅผ ์•ˆ๋ณด์ด๊ฒŒ ์ฒ˜๋ฆฌ
        slider[nextIndex].style.opacity = "1";  // ๋‘๋ฒˆ์งธ ์ด๋ฏธ์ง€๋ฅผ ๋ณด์ด๊ฒŒ ์ฒ˜๋ฆฌ

        currentIndex = nextIndex;
    }, 3000);
    // 3000 = 3์ดˆ์— ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰์‹œํ‚ด
</script>
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


Reference Book

JavaScript
HTML
CSS
๊ด‘๊ณ  ์ค€๋น„์ค‘์ž…๋‹ˆ๋‹ค.