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

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

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

charAt()์„ ํ†ตํ•ด ๊ฒ€์ƒ‰ ์‚ฌ์ดํŠธ ์ œ์ž‘ํ•˜๊ธฐ

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ ์ค‘ ํ•˜๋‚˜์ธ charAt() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์•ŒํŒŒ๋ฒณ๊ณผ css์†์„ฑ์˜ ์ฒซ๊ธ€์ž๊ฐ€ ๋™์ผํ•œ ๊ฒƒ๋งŒ ๋‚˜์˜ค๋Š” ๊ฒ€์ƒ‰ ์‚ฌ์ดํŠธ๋ฅผ ์ œ์ž‘ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!
๋จผ์ € charAt() ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ ์ดํ•ด๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด ์ „ ๊ฒŒ์‹œ๊ธ€์„ ๋ณด๊ณ  ์˜ค์‹œ๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค ;)


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

! point !
๋ฒ„ํŠผ์„ ์œ„ํ•œ ์•ŒํŒŒ๋ฒณ ๋งˆํฌ์—…์„ ์ž‘์—…ํ•ด์ค๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ์™€ css์†์„ฑ์„ ์œ„ํ•ด ์ธํฌ ์•ˆ์— keyword ๋ถ€๋ชจ ์š”์†Œ๋ฅผ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

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

<div class="search__info">
    <div class="keyword">
        <span>a</span>
        <span>b</span>
        <span>c</span>
        <span>d</span>
        <span>e</span>
        <span>f</span>
        <span>g</span>
        <span>h</span>
        <span>i</span>
        <span>j</span>
        <span>k</span>
        <span>l</span>
        <span>n</span>
        <span>m</span>
        <span>o</span>
        <span>p</span>
        <span>q</span>
        <span>r</span>
        <span>s</span>
        <span>t</span>
        <span>u</span>
        <span>v</span>
        <span>w</span>
        <span>x</span>
        <span>y</span>
        <span>z</span>
    </div>
    <div>CSS ์†์„ฑ ๊ฐฏ์ˆ˜ : <span class="num">0</span></div>
</div>

์•ŒํŒŒ๋ฒณ ๋ฒ„ํŠผ ์ž‘์—…์ด ๋๋‚˜๋ฉด ์†์„ฑ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ž‘์—…ํ•ด์ค๋‹ˆ๋‹ค.

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

<!-- ์œ„ a์•ŒํŒŒ๋ฒณ์˜ ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ์ž‘์—…ํ•ด์ค๋‹ˆ๋‹ค.(๋‚˜๋จธ์ง€๋Š” ์ƒ๋žต!) -->

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

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

โœ๏ธ ์†์„ฑ ๋ณด๊ธฐ
/* 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 */
.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 */
.search__list li {
    text-align: left;
    line-height: 1.7;
}
.search__list li.hide {
    display: none;
}
.search__list li.show {
    display: block;
}

/* search__info_type */
.search__info {
    text-align: right;
    margin-bottom: 30px;
    padding-bottom: 10px;
    border-bottom: 2px dashed var(--htmlColor);
}
.search__info .type {
    text-align: center;
    margin-bottom: 10px;
}

/* search__info_keword */
.search__info .keyword{
    text-align: center;
    margin-bottom: 10px;
}

.search__info .keyword span {
    border: 1px solid var(--htmlColor);
    border-radius: 50px;
    padding: 10px;
    display: inline-block;
    margin-bottom: 4px;
}
.search__info .keyword span:hover {
    background-color: var(--htmlColor);
    color: #fff;
    cursor: pointer;
}

/* ๋ชจ๋ฐ”์ผ */
@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 : ์Šคํฌ๋ฆฝํŠธ ์งœ๊ธฐ

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

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

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

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

//searchBox ๊ฒ€์ƒ‰ ์˜์—ญ
searchKeyword.forEach(el => {
    el.addEventListener("click", () => {
        const searchWord = el.innerText;  // ์‚ฌ์šฉ์ž๊ฐ€ ํด๋ฆญํ•œ ์•ŒํŒŒ๋ฒณ / searchWord์— ํด๋ฆญํ•œ ์•ŒํŒŒ๋ฒณ ๋ฐ์ดํ„ฐ ์ €์žฅ

        searchList.forEach(el => {
            const cssName = el.querySelector("strong").innerText; //CSS ์†์„ฑ ํ…์ŠคํŠธ / cssName์— ์…€๋ ‰ํ„ฐํ•œ ์š”์†Œ ๋ฐ์ดํ„ฐ ์ €์žฅ

            //์•ŒํŒŒ๋ฒณ ์ฒซ๊ธ€์ž์™€ css์†์„ฑ์˜ ์ฒซ๊ธ€์ž๊ฐ€ ๊ฐ™์€ ๊ฒƒ๋งŒ ์ถœ๋ ฅ๋œ๋‹ค.
            if(searchWord.charAt(0) === cssName.charAt(0)){
                el.classList.remove("hide");
            }else {
                el.classList.add("hide");
            }
        });
    });
});

! ๊ฒ€์ƒ‰ ์˜์—ญ์ด ์ž‘๋™๋˜๋Š” ๊ณผ์ • ์•Œ์•„๋ณด๊ธฐ !
***charAt(๊ฒ€์ƒ‰๊ฐ’) ๋ฉ”์„œ๋“œ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒ€์ƒ‰ํ•œ ๋ฌธ์ž์—ด์ด ์ƒ์ˆ˜์— ์ €์žฅํ•œ ๋ฐ์ดํ„ฐ ๋‚ด์— ํฌํ•จ๋˜์—ˆ์„ ๋•Œ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.***
1. forEach๋ฌธ์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ์•ŒํŒŒ๋ฒณ์„ ํด๋ฆญํ•œ ๊ฒฝ์šฐ ๋ฐ˜๋ณต๋ฌธ๊ณผ ์กฐ๊ฑด๋ฌธ์„ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.
2. classList๋ฅผ ํ†ตํ•ด hide๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ  charAt()์„ ์ด์šฉํ•˜์—ฌ css์†์„ฑ์˜ ์ฒซ๊ธ€์ž์™€ ์•ŒํŒŒ๋ฒณ์ด ๋™์ผํ•œ ๋ฆฌ์ŠคํŠธ๋Š” hide๊ฐ€ ์ง€์›Œ์ ธ ์ถœ๋ ฅ๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
(โ†’ css ์†์„ฑ๊ฐ’์œผ๋กœ hide์—๊ฒŒ display: none;์„ ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์ด ๋™์ผํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๋ณด์ด์ง€ ์•Š๊ฒŒ ๋œ๋‹ค!)


#4. ๊ฒฐ๊ณผ

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css