정보실

웹학교

정보실

javascript 위키토피아 검색창을 달기

본문

운영하는 사이트에 위키토피아 검색창을 달아보세요...

Javascript 소스


https://jsfiddle.net/ayoisaiah/Lcb53Lqs/13/


HTML :

  <main>

    <header class="searchForm-container">

      <img src="https://image.ibb.co/e6vOFQ/wikipedia.png" alt="Wikipedia Logo">

      <form class="searchForm">
        <input type="search" class="searchForm-input">
        <button type="submit" class="icon searchIcon">
          <img src="https://image.ibb.co/cpG8zk/search.png" alt="Magnifying Glass Icon">
        </button>
        <a href="https://ko.wikipedia.org/wiki/Special:Random" target="_blank" rel="noopener" class="icon randomIcon">
          <img src="https://image.ibb.co/fR5OX5/random.png" alt="Shuffle Icon">
        </a>
      </form>

    </header>

    <section class="searchResults"></section>

  </main>

CSS :

*, *::before, *::after {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

body {
  background-color: #333;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI",Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
}

.searchForm-container {
  width: 100%;
  height: 100px;
  box-shadow: 0 1px 0 rgba(0,0,0,0.15);
  display: flex;
  align-items: center;
  padding-left: 50px;
}

.searchForm {
  margin-left: 30px;
  display: flex;
  background-color: #292929;
  border: 1px solid #242424;
  border-radius: 4px;
}

.searchForm-input {
  background-color: #292929;
  border: none;
  height: 44px;
  width: 550px;
  padding: 5px 10px;
  color: white;
}

.icon {
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 44px;
  height: 44px;
  border: none;
  background-color: #292929;
}

.icon:hover {
  background-color: #1f1f1f;
}

.icon img {
  width: 22px;
  height: 22px;
}

.searchResults {
  padding: 20px 0 50px;
  margin-left: 140px;
  width: 550px;
  text-align: left;
}

.resultItem {
  opacity: 0;
  margin-bottom: 20px;
  animation: show 0.5s forwards ease-in-out;
  color: white;
  border-radius: 2px;
  padding: 10px;
  width: 100%;
}

.resultItem:hover {
  background-color: #434343;
}

.resultItem-title {
  margin-bottom: 4px;
}

.resultItem-title a {
  color: white;
  text-decoration: none;
}

.resultItem-title a:hover {
  text-decoration: underline;
}

.resultItem-snippet, .resultItem-link {
  color: #c2c2c2;
  font-size: 13px;
}

@keyframes show {
  0% {
    opacity: 0;
    transform: translateY(100px);
  }

  100% {
    opacity: 1;
    transform: translateY(0);
  }
}


@media screen and (max-width: 750px) {

  .searchForm-container {
    padding: 20px 0 20px;
    height: auto;
    justify-content: space-between;  
    flex-direction: column;
  }

  .searchForm {
    margin: 20px auto 0;
  }

  .searchForm-input {
    width: 300px;
  }

  .searchResults {
    width: 388px;
    margin-left: auto;
    margin-right: auto;
  }
}


Javascript :

function handleSubmit(event) {
    // prevent page from reloading when form is submitted
  event.preventDefault();
  // get the value of the input field
  const input = document.querySelector('.searchForm-input').value;
  // remove whitespace from the input
  const searchQuery = input.trim();
  // call `fetchResults` and pass it the `searchQuery`
  fetchResults(searchQuery);
}

function fetchResults(searchQuery) {
	  const endpoint = `https://ko.wikipedia.org/w/api.php?action=query&list=search&prop=info&inprop=url&utf8=&format=json&origin=*&srlimit=20&srsearch=${searchQuery}`;
  	fetch(endpoint)
  		.then(response => response.json())
  		.then(data => {
  	  	const results = data.query.search;
        displayResults(results);
		})
    .catch(() => console.log('An error occurred'));
}

function displayResults(results) {
  // Store a reference to `.searchResults`
  const searchResults = document.querySelector('.searchResults');
  // Remove all child elements
  searchResults.innerHTML = '';
  // Loop over results array
  results.forEach(result => {
   const url = encodeURI(`https://ko.wikipedia.org/wiki/${result.title}`);

   searchResults.insertAdjacentHTML('beforeend',
      `<div class="resultItem">
        <h3 class="resultItem-title">
          <a href="${url}" target="_blank" rel="noopener">${result.title}</a>
        </h3>
        <span class="resultItem-snippet">${result.snippet}</span><br>
        <a href="${url}" class="resultItem-link" target="_blank" rel="noopener">${url}</a>
      </div>`
    );
  });
}

const form = document.querySelector('.searchForm');
form.addEventListener('submit', handleSubmit);


페이지 정보

조회 86회 ]  작성일18-06-08 22:50

웹학교