Blinking Hello Kitty Angel

javascript

자바스크립트 검색 효과 첫 번째 만들기

xoouxa 2023. 3. 23. 20:29

“ 지연되는 프로젝트에 인력을 더 투입하면 오히려 더 늦어진다. ”

- Frederick Philips Brooks
Mythical Man-Month 저자
728x90

안녕하세요 ヾ(≧▽≦*)o 오늘은 내가 원하는대로 푸터 유형의 페이지를 만드는 작업을 한 번

해보도록 하겠습니다.

 

검색 이펙트에 적용되는 자바스크립트 속성

- 데이터 저장하기 : 변수

- 데이터 실행하기 : 화살표 함수

- 데이터 불러오기 : for()문, forEach()문

-배열 객체 : indexOf(), search()

- 요소 객체 :  querySelector(), querySelectorAll()

- 요소 메서드 : element.style.속성명 : CSS 스타일 설정

- 요소 속성 : textContent, dataset

- 이벤트 객체 : keyup

 

기본 reset css 설정

@font-face {
    font-family: 'CookieRun';
    font-weight: 400;
    font-style: normal;
    src: url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunRegular.eot');
    src: url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunRegular.eot?#iefix') format('embedded-opentype'),
         url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunRegular.woff2') format('woff2'),
         url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunRegular.woff') format('woff'),
         url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunRegular.ttf') format("truetype");
    font-display: swap;
} 
@font-face {
    font-family: 'CookieRun';
    font-weight: 700;
    font-style: normal;
    src: url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunBold.eot');
    src: url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunBold.eot?#iefix') format('embedded-opentype'),
         url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunBold.woff2') format('woff2'),
         url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunBold.woff') format('woff'),
         url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunBold.ttf') format("truetype");
    font-display: swap;
} 
@font-face {
    font-family: 'CookieRun';
    font-weight: 900;
    font-style: normal;
    src: url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunBlack.eot');
    src: url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunBlack.eot?#iefix') format('embedded-opentype'),
         url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunBlack.woff2') format('woff2'),
         url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunBlack.woff') format('woff'),
         url('https://cdn.jsdelivr.net/gh/webfontworld/cookierun/CookieRunBlack.ttf') format("truetype");
    font-display: swap;
} 
*, *:before, *:after {
    margin:0;
    padding:0;
    box-sizing:border-box;
  }
  html,
  body {
    width: 100%;
    height: 100%;
    font-family: 'CookieRun';
  }
  body.pink {
    background-color: #FFD9F4;
    background-image:
      linear-gradient(90deg, #FFB9EA 0px, #FFB9EA 1px, transparent 1px, transparent 99px,  transparent 100px),
      linear-gradient(#FFB9EA 0px, #FFB9EA 1px, transparent 1px, transparent 99px,  transparent 100px),
      linear-gradient(#FFB9EA 0px, #FFB9EA 1px, transparent 1px, transparent 99px,  transparent 100px),
      linear-gradient(90deg, #FFB9EA 0px, #FFB9EA 1px, transparent 1px, transparent 99px,  transparent 100px),
      linear-gradient(transparent 0px, transparent 5px, #FFD9F4 5px, #FFD9F4 95px, transparent 95px, transparent 100px),
      linear-gradient(90deg, #FFB9EA 0px, #FFB9EA 1px, transparent 1px, transparent 99px, #FFB9EA 99px, #FFB9EA 100px),
      linear-gradient(90deg, transparent 0px, transparent 5px, #FFD9F4 5px, #FFD9F4 95px, transparent 95px, transparent 100px),
      linear-gradient(#FFB9EA, #FFB9EA);
    background-size:100px 100%, 100% 100px, 100% 10px, 10px 100%, 100% 100px, 100px 100%, 100px 100%, 100px 100px, 100px 100px;
  }
  body.yellow {
    background-color: #FFEF4A;
    background-image:
        linear-gradient(90deg, #E6D535 0px, #E6D535 1px, transparent 1px, transparent 99px, transparent 100px),
        linear-gradient(#E6D535 0px, #E6D535 1px, transparent 1px, transparent 99px, transparent 100px),
        linear-gradient(#E6D535 0px, #E6D535 1px, transparent 1px, transparent 99px, transparent 100px),
        linear-gradient(90deg, #E6D535 0px, #E6D535 1px, transparent 1px, transparent 99px, transparent 100px),
        linear-gradient(transparent 0px, transparent 5px, #FFEF4A 5px, #FFEF4A 95px, transparent 95px, transparent 100px),
        linear-gradient(90deg, #E6D535 0px, #E6D535 1px, transparent 1px, transparent 99px, #E6D535 99px, #E6D535 100px),
        linear-gradient(90deg, transparent 0px, transparent 5px, #FFEF4A 5px, #FFEF4A 95px, transparent 95px, transparent 100px),
        linear-gradient(#E6D535, #E6D535);
    background-size: 100px 100%, 100% 100px, 100% 10px, 10px 100%, 100% 100px, 100px 100%, 100px 100%, 100px 100px, 100px 100px;
  }

 

구조 

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>검색 이펙트</title>
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/search.css">
</head>
<body class="pink">
    <header id="header">
        <ul>
            <li class="active"><a href="mouseEffect01.html">1</a></li>
            <li><a href="mouseEffect02.html">2</a></li>
            <li><a href="mouseEffect03.html">3</a></li>
            <li><a href="mouseEffect04.html">4</a></li>
            <li><a href="mouseEffect05.html">5</a></li>
            <li><a href="mouseEffect06.html">6</a></li>
        </ul>
    </header>
    <!-- //header -->
    <main id="main">
        <div class="search__wrap">
            <div class="search__header">
                <h2 class="title">자바스크립트 💜</h2>
            </div>
            <div class="search__conts">
                <hgroup>
                    <h3>CSS 속성 검색하기</h3>
                    <h4>indexOf() / search()</h4>
                </hgroup>
                <div class="search__box">
                    <label for="search">검색하기</label>
                    <input type="text" name="search" id="search" placeholder="CSS 속성을 입력해주세요.😊">
                </div>
                <div class="search__info">
                    <div>
                        CSS 속성 갯수 : <span>0</span>개
                    </div>
                </div>
                <div class="search__list">
                    <ul>
                        <li data-name="align-content"><strong>align-content</strong> : 콘텐츠 아이템의 상하관계 정렬 상태를 설정합니다.</li>
                        <li data-name="align-items"><strong>align-items</strong> : 콘텐츠 아이템의 내부 상하관계 정렬 상태를 설정합니다.</li>
                        <li data-name="align-self"><strong>align-self</strong> : 개별적인 콘텐츠 아이템의 정렬 상태를 설정합니다.</li>
                        <li data-name="all"><strong>all</strong> : 요소의 속성을 초기화 또는 상속을 설정합니다.</li>
                        </ul>
                </div>
            </div>
        </div>
    </main>
    <footer id="footer">
        <a href="mailto:yuna243441@naver.com">yuna243441@naver.com</a>
    </footer>
    </body>

search__wrap 안에 search__header와 conts, box, info, list를 만들어 줍니다.

li에 data-name을 붙여 각 속성들의 이름을 달아 속성을 적어줍니다.

 

search CSS 설정

#header {
    padding: 1.4vw 1.4vw 2vw 1.4vw;
    text-align: center;
}
#header li {
    list-style: none;
    display: inline;
}
#header li a {
    text-decoration: none;
    width: 50px;
    height: 50px;
    line-height: 40px;
    display: inline-block;
    border-radius: 50%;
    text-align: center;
    color: #fff;
    border: 5px solid #fff;
    transition: background-color 0.3s;
}
#header li:nth-child(3n+1) a {
    background-color: #AF6EE3;
    border-color: #9659C9;
    color: #EEDAFF
}
#header li:nth-child(3n+1) a:hover {
    background-color: #9659C9;
}
#header li:nth-child(3n+2) a {
    background-color: #FFEF4A;
    border-color: #F2C81E;
    color: #675E3B;
}
#header li:nth-child(3n+2) a:hover {
    background-color: #F2C81E;
}
#header li:nth-child(3n+3) a {
    background-color: #F76ECE;
    border-color: #C04D9E;
    color: #FFDEF5;
}
#header li:nth-child(3n+3) a:hover {
    background-color: #C04D9E;
}
/* main */
#main {}
/* search__wrap */
.search__wrap {
    margin: 0 auto;
    width: 90%;
    margin: 5% auto;
    background-color: #fff;
    border: 0.3vw solid #000;
    border-top-left-radius: 3vw;
    border-bottom-right-radius: 3vw;
    box-shadow: 1vw 1vw 0px #000;
    transition: box-shadow 0.3s;
    overflow: hidden;
}
.search__wrap:hover {
    box-shadow: 0.5vw 0.5vw 0px #000;
}
.search__header {
    border-bottom: 0.3vw solid #000;
    padding: 0 0 0 3vw;
    background-color: #AF6EE3;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.search__header::before {
    content: "";
    width: 90%;
    height: 0.3vw;
    background: #000;
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-10px);
    border-radius: 5px;
}
.search__header::after {
    content: "";
    width: 90%;
    height: 0.3vw;
    background: #000;
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(4px);
    border-radius: 5px;
}
.search__header .title {
    position: relative;
    background: #AF6EE3;
    position: relative;
    z-index: 10;
    padding-right: 35px;
    padding: 0.6vw 8vw 0.6vw 5vw;
}
.search__header .btn {
    display: flex;
}
.search__header .btn span {
    display: block;
    width: 3vw;
    height: 3vw;
    line-height: 3.9vw;
    position: relative;
    z-index: 10;
    border-left: 0.3vw solid #000;
    box-sizing: content-box;
    text-align: center;
    font-size: 2vw;
    cursor: pointer;
}
.search__conts hgroup {
    text-align: center;
    padding: 3vw 5vw;
    border-bottom: 0.3vw solid #000;
}
.search__conts hgroup h3 {
    color: #FFEF4A;
    font-size: 8vw;
    text-shadow: 0.4vw 0.4vw 0px #000;
    -webkit-text-stroke: 0.2vw rgb(0, 0, 0);
}
.search__conts hgroup h4 {
    color: #AF6EE3;
    font-size: 6vw;
    text-shadow: 0.4vw 0.4vw 0px #000;
    -webkit-text-stroke: 0.2vw rgb(0, 0, 0);
}
.search__box {
    text-align: center;
    padding: 1vw;
    border-bottom: 0.3vw dashed #000;
}
.search__box label {
    padding: 1vw;
}
.search__box input {
    border: 0.3vw solid #000;
    box-shadow: 0.3vw 0.3vw 0 #000;
    width: 80%;
    padding: 1vw 2vw;
    border-radius: 50px;
    font-family: 'cookieRun';
    outline: none;
}
.search__list ul {
    padding: 3vw;
}
.search__info {
    text-align: right;
    padding: 0.1vw 2vw;
    border-top: 0.3vw dashed #000;
    border-bottom: 0.3 dashed #000;
}
.search__list li {
    list-style: none;
    line-height: 2;
}
.search__list li.hide {
    display: none;
}
/* footer */
#footer {
    text-align: center;
    padding: 3vw;
}
#footer a {
    color: #000;
}

 

스크립트

 <script>
        //선택자
        const searchBox = document.querySelector(".search__box input");     //검색
        const searchList = document.querySelectorAll(".search__list li");   //목록 리스트
        const searchInfo = document.querySelector(".search__info span");
        searchInfo.textContent = searchList.length

        //검색
        searchBox.addEventListener("keyup", () => {
            const userWord = searchBox.value;   //사용자가 입력한 키워드를 가져올 때 value를 씀

            searchList.forEach((el,index) => {
                // const cssName = el.getAttribute("data-name");
                const cssName = el.dataset.name;
                if(cssName.indexOf(userWord)){
                    //데이터 있으면
                    el.classList.add("hide");
                } else {
                    //데이터 없을 때
                    el.classList.remove("hide");
                }
            })
        });
    </script>

searchBox라는 변수에 .search__box input 선택자를 지정해 줍니다.

그리고 searchList라는 변수에 .search__list li를 선택자로 지정해 줍니다.

searchBox에 addEventListener으로 이벤트를 추가해 줍니다.

 

잘 모르는 속성

속성 설명
keyup key up은 자바스크립트 이벤트 중 하나입니다. 이는 사용자가 키보드에서 어떤 키를 뗄 때 발생합니다.
dataset HTML 요소의 data- 접두사를 가진 사용자 정의 데이터 속성에 엑세스 하는데 사용됩니다.
text-align 속성은 텍스트 정렬 방식을 설정합니다.
vertical-align 인라인 요소의 수직 정렬 방법을 설정하는 속성입니다.
justify-content CSS Flexbox 레아이웃에서 주 축을 따라 플렉스 아이템을 정렬하는 속성입니다.
background-repeat 배경 이미지 반복 여부를 설정하는 속성입니다.
background-position 배경 이미지의 위치를 지정하는 속성입니다.

 

🥰 오늘도 감사합니다.