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

[Search Effect] ๊ฒ€์ƒ‰ ์ดํŽ™ํŠธ 04

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

๊ฒ€์ƒ‰ ์ดํŽ™ํŠธ ๋งŒ๋“ค๊ธฐ 04

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๊ทธ ๋™์•ˆ ๋ฐฐ์› ๋˜ JavaScript๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰ ์ดํŽ™ํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
์ด๋ฒˆ ํ•™์Šต ๋‚ด์šฉ์„ ์ž˜ ํ™œ์šฉํ•˜์—ฌ ๊ทธ๋™์•ˆ ๋ฐฐ์› ๋˜ CSS ์†์„ฑ๋“ค์„ ๊ฒ€์ƒ‰์ฐฝ ํ˜•์‹์œผ๋กœ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•ฉ์‹œ๋‹ค. ๋‚˜์ค‘์— ๋ชจ๋ฅด๋Š” ๊ฒŒ ์ƒ๊ฒผ์„ ๋•Œ ๊ณต๋ถ€ํ•˜๊ธฐ ํŽธํ•˜๊ฒŒ ๋ง์ด์ฃ !

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


HTML ์†Œ์Šค ๋ณด๊ธฐ

๊ฒ€์ƒ‰ ์ดํŽ™ํŠธ 04์˜ HTML ์†Œ์Šค์™€ ๋ถ€์—ฐ ์„ค๋ช…(์ฃผ์„ ์ฒ˜๋ฆฌ๋จ)์ด๋‹ค.
CSS ์†Œ์Šค๋ฅผ ํ•จ๊ป˜ ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด ๋งจ ์•„๋ž˜์˜ 'CSS ์†Œ์Šค ๋ณด๊ธฐ' ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๊ฒƒ.

<body>
    <header id="header">
        <h1>Search</h1>
        <nav>
            <ul>
                <li><a href="searchEffect01.html">01</a></li>
                <li><a href="searchEffect02.html">02</a></li>
                <li><a href="searchEffect03.html">03</a></li>
                <li class="active"><a href="searchEffect04.html">04</a></li>
                <li><a href="searchEffect05.html">05</a></li>
                <li><a href="searchEffect06.html">06</a></li>
            </ul>
        </nav>
    </header>
    <!-- // header -->

    <main id="main">
        <div class="search__wrap">
            <span>find()๋ฅผ ์ด์šฉํ•˜์—ฌ ์†์„ฑ์„ ๊ฒ€์ƒ‰ํ•˜๋ฉด ์„ค๋ช… ๋ณด์—ฌ์ฃผ๊ธฐ</span>
            <h1>CSS ์†์„ฑ ๊ฒ€์ƒ‰ํ•˜๊ธฐ</h1>
            <div class="search__box">
                <label for="search">๊ฒ€์ƒ‰ํ•˜๊ธฐ</label>
                <input type="text" id="search" placeholder="CSS ์†์„ฑ ๋ฐ ์œ ํ˜•์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.">
            </div>

            <div class="search__desc">
                ์†์„ฑ์„ ๊ฒ€์ƒ‰ํ•˜์‹œ๋ฉด ์„ค๋ช…์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.
            </div>

            <div class="search__info">
                <div>์ „์ฒด ์†์„ฑ ๊ฐœ์ˆ˜ : <span class="num">0</span></div>
            </div>

            <div class="search__list">
                     
            </div>
        </div>
    </main>
    <!-- // main -->

    <footer id="footer">
        <a href="mailto:kde66034@gmail.com">kde66034@gmail.com</a>
    </footer> 
    <!-- //footer -->
</body>

JavaScript ์†Œ์Šค ๋ณด๊ธฐ

๊ฒ€์ƒ‰ ์ดํŽ™ํŠธ 04์˜ JavaScript ์†Œ์Šค์™€ ๋ถ€์—ฐ ์„ค๋ช…(์ฃผ์„ ์ฒ˜๋ฆฌ๋จ)์ด๋‹ค. ์ƒ์ˆ˜ cssProperty์— ๊ฒ€์ƒ‰๊ฒฐ๊ณผ์— ๋„์šธ CSS ์†์„ฑ ๋ฆฌ์ŠคํŠธ๋“ค์„ ๋ชจ๋‘ ์ €์žฅํ•˜๊ณ  ์ด๊ฒƒ๋“ค์„ ์ถœ๋ ฅํ•˜๊ณ  ์†์„ฑ ํด๋ฆญ ์‹œ ์„ค๋ช…์ด ๋‚˜์˜ค๋„๋ก ํ•˜๋Š” ์ž‘์—…๊ณผ ๋งˆ์ง€๋ง‰์œผ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ์†์„ฑ name์˜ desc๋ฅผ ์ถœ๋ ฅ์‹œ์ผœ์ฃผ๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ง ๋‹ค.

// CSS ์†์„ฑ ๋ฆฌ์ŠคํŠธ ์ €์žฅ
const cssProperty = [
    { name: "accent-color", desc: "ํŠน์ • ์š”์†Œ์— ์ƒ‰์ƒ์„ ์ง€์ •ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค." },
    { name: "align-content", desc: "์ฝ˜ํ…์ธ  ์•„์ดํ…œ์˜ ์ƒํ•˜๊ด€๊ณ„ ์ •๋ ฌ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค." },
    { name: "align-items", desc: "์ฝ˜ํ…์ธ  ์•„์ดํ…œ์˜ ๋‚ด๋ถ€ ์ƒํ•˜๊ด€๊ณ„ ์ •๋ ฌ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค." },
]

const searchList = document.querySelector(".search__list");         // ์„œ์น˜ ๋ฆฌ์ŠคํŠธ ul
const searchNum = document.querySelector(".search__info .num");        // ์ „์ฒด ๊ฐœ์ˆ˜
const searchBox = document.querySelector(".search__box input");        // ๊ฒ€์ƒ‰์ฐฝ
const searchDesc = document.querySelector(".search__desc");            // ์„ค๋ช…์ฐฝ

// ์ถœ๋ ฅํ•˜๊ธฐ
cssProperty.map((element, index) => {
    searchNum.innerHTML = cssProperty.length; // CSS ์†์„ฑ ์ด ๊ฐœ์ˆ˜ ์ถœ๋ ฅ
    // searchNum.innerHTML = index + 1;

    searchList.innerHTML += `${element.name}`;
});

// ์†์„ฑ ํด๋ฆญ ์ž‘์—…
const searchListSpan = searchList.querySelectorAll('span');
searchListSpan.forEach((span) => {
    span.addEventListener('click', (e) => {
        const seachWord = e.target.textContent;
        findProperty(seachWord);
        window.scrollTo({left : 0, top : 0, behavior : "smooth"});
    });
});

// ์ž…๋ ฅํ•˜๊ธฐ
searchBox.addEventListener("keyup", () => {
    const searchWord = searchBox.value;     // ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ์ €์žฅ
    // console.log(searchWord);

    findProperty(searchWord);
});

// ์†์„ฑ ์ฐพ๊ธฐ
function findProperty(searchData) {
    const targetData = cssProperty.find((data) => data.name === searchData);

    if(targetData == null) {    // ๋ฐ์ดํ„ฐ๊ฐ€ null์ผ(์กด์žฌํ•˜์ง€ ์•Š์„) ๊ฒฝ์šฐ
        searchDesc.textContent = "ํ•ด๋‹น ์†์„ฑ์€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๊ฒ€์ƒ‰ํ•ด์ฃผ์„ธ์š”!";   // ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ์‹œ ํ‘œ์‹œํ•  ๋ฌธ๊ตฌ
        return;     // ์ข…๋ฃŒ์˜ ์˜๋ฏธ - F12 ์ฝ˜์†”์ฐฝ ์˜ค๋ฅ˜๋ฅผ ์—†์• ์คŒ
    };

    searchDesc.textContent = targetData.desc;
};
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


Reference Book

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