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

[Search] ๊ฒ€์ƒ‰ ์œ ํ˜• ์‚ฌ์ดํŠธ ๋งŒ๋“ค๊ธฐ01

by _ํ† ๋งคํ†  2022. 8. 17.
728x90

indexOf()๋ฅผ ํ†ตํ•ด ๊ฒ€์ƒ‰ ์‚ฌ์ดํŠธ ์ œ์ž‘ํ•˜๊ธฐ

์ง€๋‚œ ์‹œ๊ฐ„์—๋Š” indexOf()์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์‹ฌํ™”๊ณผ์ •์œผ๋กœ ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜์ธ indexOf() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฒ€์ƒ‰ ์‚ฌ์ดํŠธ๋ฅผ ์ œ์ž‘ํ•ด๋ณผ๊นŒ์š”?!
๋จผ์ € indexOf() ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด ์งง๊ฒŒ ์•Œ์•„๋ณด๊ณ  ์‚ฌ์ดํŠธ ์‹ค์Šต์— ๋“ค์–ด๊ฐ€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!


#1. indexOf()

indexOf() ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ฌธ์ž์˜ ์œ„์น˜(index)๋ฅผ ์ฐพ์€ ํ›„ ์ˆซ์ž๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

โœ๏ธ์ฝ”๋“œ ๋ณด๊ธฐ

const str1 = "javascript reference"
            
const currentStr1 = str1.indexOf("javascript"); // 0
const currentStr2 = str1.indexOf("reference");  // 11 (11๋ฒˆ์งธ ์ž๋ฆฌ : ์œ„์น˜ ๊ฐ’ ์ถœ๋ ฅ)
const currentStr3 = str1.indexOf("j");          // 0
const currentStr4 = str1.indexOf("a");          // 1 (์ค‘๋ณต์ผ ๋•Œ ๊ฐ€์žฅ ์ฒ˜์Œ ๊ฒƒ๋งŒ ์ถœ๋ ฅ)
const currentStr5 = str1.indexOf("v");          // 2
const currentStr6 = str1.indexOf("jquery");     // -1

#2. HTML : ๊ตฌ์กฐ ์งœ๊ธฐ

! point ! list ํƒœ๊ทธ์™€ data-name ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ด๋ฆ„์„ ์ •ํ•ด๋ณด์ž!

โœ๏ธ์ฝ”๋“œ ๋ณด๊ธฐ

<div class="search__wrap">
    <span>indexOf()๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰ํ•˜๊ธฐ</span>
    <h1>HTML ํƒœ๊ทธ ๊ฒ€์ƒ‰ํ•˜๊ธฐ</h1>
    <div class="search__box">
        <label for="search">๊ฒ€์ƒ‰ํ•˜๊ธฐ</label>
        <input type="text" id="search" placeholder="HTML ํƒœ๊ทธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!">
    </div>
    <div class="search__list">
        <div class="html">
            <ul>
            <li data-name="accent-color"><strong>accent-color</strong> : accent-color๋Š” ์š”์†Œ์˜ ๊ฐ•์กฐ ์ƒ‰์ƒ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="align-content"><strong>align-content</strong> : align-content๋Š” ์ฝ˜ํ…์ธ  ์‚ฌ์ด์™€ ์ฝ˜ํ…์ธ  ์ฃผ์œ„ ๋นˆ ๊ณต๊ฐ„์„ ํ”Œ๋ ‰์Šค ๋ฐ•์Šค'์˜ ๊ต์ฐจ์ถ• ๋˜๋Š” ๊ทธ๋ฆฌ๋“œ์˜ ๋ธ”๋ก ์ถ•์„ ๋”ฐ๋ผ ๋ฐฐ์น˜ํ•˜๋Š” ๋ฐฉ์‹์„ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="align-items"><strong>align-items</strong> : align-item์€ ์ฝ˜ํ…์ธ  ์•„์ดํ…œ์˜ ๋‚ด๋ถ€ ์ƒํ•˜๊ด€๊ณ„ ์ •๋ ฌ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="align-self"><strong>align-self</strong> : align-self๋Š” ๊ฐœ๋ณ„์  ์ฝ˜ํ…์ธ  ์•„์ดํ…œ์˜ ์ •๋ ฌ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="allr"><strong>all</strong> : all์€ ์š”์†Œ์˜ ๋ชจ๋“  ์†์„ฑ์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ๋˜๋Š” ์ƒ์†์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="animation"><strong>animation</strong> : animation์€ ์• ๋‹ˆ๋ฉ”์ด์…˜๊ณผ ๊ด€๋ จ๋œ ์†์„ฑ์„ ์ผ๊ด„์ ์œผ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="animation-delay"><strong>animation-delay</strong> : animation-delay๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ง€์—ฐ ์‹œ๊ฐ„์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="animation-direction"><strong>animation-direction</strong> : animation-direction๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ ์›€์ง์ž„ ๋ฐฉํ–ฅ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="animation-duration"><strong>animation-duration</strong> : animation-duration๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ ์›€์ง์ž„ ์‹œ๊ฐ„์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="animation-fill-mode"><strong>animation-fill-mode</strong> : animation-fill-mode๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ๋๋‚œ ํ›„์˜ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="animation-iteration-count"><strong>animation-iteration-count</strong> : animation-iteration-count๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="animation-name"><strong>animation-name</strong> : animation-name์€ ์• ๋‹ˆ๋ฉ”์ด์…˜ Keyframe ์ด๋ฆ„์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="animation-play-state"><strong>animation-play-state</strong> : animation-play-state๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์˜ ์ง„ํ–‰ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="animation-timeline"><strong>animation-timeline</strong> : animation-timeline์€ ์š”์†Œ์— ์ ์šฉํ•  ์Šคํฌ๋กค ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์„ค๋ช…ํ•˜๋Š” ํ•˜๋‚˜ ์ด์ƒ์˜ @scroll-timeline at-rules ์ด๋ฆ„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="animation-timing-function"><strong>animation-timing-function</strong> : animation-timing-function์€ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์›€์ง์ž„์˜ ์†๋„๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.</li>
            <li data-name="appearance"><strong>appearance</strong> : appearance๋Š” webit ๊ณ„์—ด์˜ ๋ธŒ๋ผ์šฐ์ €์˜ type="search" ํ•„๋“œ์˜ ๋‘ฅ๊ทผ ํ…Œ๋‘๋ฆฌ ๊ฐ’์ด๋‚˜ reset ํšจ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฒ„ํŠผ์„ ์‚ญ์ œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.</li>
            <li data-name="aspect-ratio"><strong>aspect-ratio</strong> : aspect-ratio๋Š” ์š”์†Œ์˜ ํฌ๊ธฐ๋ฅผ ๋น„์œจ๋Œ€๋กœ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.</li>
            </ul>
        </div>
    </div>
</div>

#3. CSS : ์Šคํƒ€์ผ์ฃผ๊ธฐ

๋งˆํฌ์—…์„ ํ† ๋Œ€๋กœ ์Šคํƒ€์ผ ์†์„ฑ์„ ์„ค์ •ํ•ด๋ณด์ž!
- ์Šคํƒ€์ผ ์†์„ฑ์€ ์ •ํ•ด์ง„ ๊ฒƒ์ด ์•„๋‹ˆ๋‹ˆ ์‹ค์Šตํ•˜์‹ค ๋ถ„๋“ค์€ ์ง์ ‘ ์†์„ฑ์„ ์•Œ์•„๋ณด๊ณ  ์Šคํƒ€์ผ์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค!

โœ๏ธ์†์„ฑ ๋ณด๊ธฐ

/* font color */
:root {
    --htmlColor : #223547;
    --cssColor : #472237;
    --javascriptColor : #624313;
}

/* reset */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'NexonLv1Gothic';
    color: var(--htmlColor);
}
*, *:before, *:after {
    box-sizing:border-box;
}
a {
    color: var(--htmlColor);
    text-decoration: none;
}
li {
    list-style: none;
}

/* header */
#header nav {
    margin: 10px;
}
#header nav li {
    display: inline;
    position: relative;
}
#header nav li a {
    width: 30px;
    height: 30px;
    border: 1px solid var(--htmlColor);
    border-radius: 50%;
    display: inline-block;
    text-align: center;
    line-height: 30px;
    font-family: 'NexonLv1Gothic';
}
#header nav li.active a {
    background-color: var(--htmlColor);
    color: #fff;
}
#header nav li .sub {
    position: absolute;
    left: 0;
    top: 35px;
    width: 400px;
}
#header nav li .sub li a {
    width: auto;   
    background-color: transparent;
    color: var(--htmlColor);
    border: 0;
    text-align: left;
    line-height: 1.2;
}
#header nav li .sub li.active a {
    text-decoration: underline;
}


/* main */
#main {
    margin: 50px 10px;
}
.search__wrap {
    max-width: 1400px;
    margin: 0 auto;
    border: 3px solid var(--htmlColor);
    border-radius: 20px;
    background-color: #F1F3F6;
    padding: 30px;
    text-align: center;
}
.search__wrap > span {
    font-size: 20px;
    margin-bottom: 20px;
    display: inline-block;
}
.search__wrap > h1 {
    font-family: 'Tmon';
    color: var(--htmlColor);
    font-size: 6vw;
    margin-bottom: 10px;
}
.search__box {
    margin-bottom: 40px;
}
/* irํšจ๊ณผ */
.search__box label {
    position: absolute;
    clip: rect(0 0 0 0);
    width: 1px;
    height: 1px;
    margin: -1px;
    overflow: hidden;
}
.search__box input {
    border: 2px solid var(--htmlColor);
    padding: 15px 40px;
    width: 70%;
    border-radius: 50px;
    font-size: 20px;
}
.search__list li {
    text-align: left;
    line-height: 1.7;
}
.search__list li.hide {
    display: none;
}

/* ๋ชจ๋ฐ”์ผ */
@media (max-width: 600px){
    .search__wrap {
        padding: 20px;
    }
    .search__wrap > span {
        font-size: 16px;
        margin-bottom: 10px;
    }
    .search__wrap > h1 {
        font-size: 44px;
    }
    .search__box input {
        font-size: 16px;
        padding: 12px 30px;
    }
}

/* footer */
#footer {
    text-align: center;
}
#footer a {
    color: #000;
    font-family: 'NexonLv1Gothic';
    padding-bottom: 50px;
}
#footer a:hover {
    text-decoration: underline;
}

#4. JAVASCRIPT : ์Šคํฌ๋ฆฝํŠธ ์งœ๊ธฐ

indexOf()์™€ ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์™„์„ฑํ•ด๋ณด์ž!
์ฝ”๋“œ์˜ ์ฃผ์„๊ณผ ํ•จ๊ป˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ดํ•ดํ•ด๋ณด๋„๋ก ํ•ฉ๋‹ˆ๋‹ค:)

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

//์„ ํƒ์ž
const searchBox = document.querySelector(".search__box input");           //๊ฒ€์ƒ‰ ์˜์—ญ
const searchList = document.querySelectorAll(".search__list ul li");     //๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ
const searchInfo = document.querySelector(".search__info .num");        //๊ฐฏ์ˆ˜

//์ „์ฒด ๊ฐฏ์ˆ˜ ๊ตฌํ•˜๊ธฐ
searchInfo.textContent = searchList.length;

//๊ฒ€์ƒ‰ ์˜์—ญ
searchBox.addEventListener("keyup", () => {
    const searcWord = searchBox.value; //์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ํ‚ค์›Œ๋“œ

    searchList.forEach(el => {
        const cssName = el.dataset.name;


        //0์ด ๋˜์—ˆ์„ ๋•Œ false๊ฐ€ ๋œ ๊ฐ’์„ ๋ณด์ด๊ฒŒ ํ•œ๋‹ค. keyup์„ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•œ ๊ธ€์ž๋งŒ ์ž‘์„ฑํ•ด๋„ ์‹คํ–‰.
        if(cssName.indexOf(searcWord)){
            el.classList.add("hide");      //๊ฒ€์ƒ‰ํ•œ ๋‹จ์–ด ์™ธ์˜ ๋‹จ์–ด ์ˆจ๊น€
        } else {
            el.classList.remove("hide");   //๊ฒ€์ƒ‰ํ•œ ๋‹จ์–ด๋ฅผ ๋ณด์ด๊ฒŒ ํ•จ : indexOf()๊ฐ’์ด 0(=false)์ด ๋˜๋Š” ๊ฐ’๋งŒ hide๋ฅผ ์ง€์›Œ์ค€๋‹ค.
        }
    });
});

! point ! indexOf()๊ฐ’์ด 0(=false)์ด ๋˜๋Š” ๊ฒƒ๋งŒ hide๋ฅผ ์ง€์›Œ์ค€๋‹ค.


#4. ๊ฒฐ๊ณผ

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css