비전공자 개발일기

Dictionary App 본문

Javascript

Dictionary App

HiroDaegu 2022. 12. 1. 10:32
728x90
SMALL

<!DOCTYPE html>
<html lang="en">

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" />
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet" />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
    <!-- Script -->
    <script defer src="main.js"></script>
    <title>Dictionary App</title>
</head>

<body>
    <audio id="sound"></audio>
    <div class="container">
        <div class="search-box">
            <input type="text" placeholder="Type the word here.." id="inp-word" />
            <button id="search-btn">Search</button>
        </div>
        <div class="result" id="result"></div>
    </div>

</body>

</html>
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
  }
  *:not(i) {
    font-family: "Poppins", sans-serif;
  }
  body {
    height: 100vh;
    background: linear-gradient(184deg, #8754ff, #8e2de2);
  }
  .container {
    background-color: #d2daff;
    width: 90vmin;
    position: absolute;
    transform: translate(-50%, -50%);
    top: 50%;
    left: 50%;
    padding: 80px 50px;
    border-radius: 10px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
  }
  .search-box {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
  .search-box input {
    padding: 5px;
    width: 70%;
    border: none;
    outline: none;
    border-bottom: 3px solid #8754ff;
    font-size: 16px;
    background-color: #d2daff;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
  }
  .search-box button {
    padding: 15px 0;
    width: 25%;
    background-color: #8754ff;
    border: none;
    outline: none;
    color: #fff;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    border-radius: 5px;
  }
  .result {
    position: relative;
  }
  .result h3 {
    font-size: 30px;
    color: rgb(77, 76, 76);
  }
  .result .word {
    display: flex;
    justify-content: space-between;
    margin-top: 80px;
  }
  .result button {
    background-color: transparent;
    color: #8754ff;
    border: none;
    outline: none;
    font-size: 18px;
  }
  .result .details {
    display: flex;
    gap: 10px;
    color: rgb(82, 81, 81);
    margin: 5px 0 20px 0;
    font-size: 14px;
  }
  .word-meaning {
    color: #000;
  }
  .word-example {
    color: rgb(82, 81, 81);
    font-style: italic;
    border-left: 5px solid #8754ff;
    padding-left: 20px;
    margin-top: 30px;
  }
  .error {
    margin-top: 80px;
    text-align: center;
  }
const url = "https://api.dictionaryapi.dev/api/v2/entries/en/";
const result = document.getElementById("result");
const sound = document.getElementById("sound");
const btn = document.getElementById("search-btn");

btn.addEventListener("click", () => {
  let inpWord = document.getElementById("inp-word").value;
  fetch(`${url}${inpWord}`)
    .then((response) => response.json())
    .then((data) => {
      console.log(data);
      result.innerHTML = `
            <div class="word">
                    <h3>${inpWord}</h3>
                    <button onclick="playSound()">
                        <i class="fas fa-volume-up"></i>
                    </button>
                </div>
                <div class="details">
                    <p>${data[0].meanings[0].partOfSpeech}</p>
                    <p>/${data[0].phonetic}/</p>
                </div>
                <p class="word-meaning">
                   ${data[0].meanings[0].definitions[0].definition}
                </p>
                <p class="word-example">
                    ${data[0].meanings[0].definitions[0].example || ""}
                </p>`;
      sound.setAttribute("src", `${data[0].phonetics[0].audio}`);
    })
    .catch(() => {
      result.innerHTML = `<h3 class="error">Couldn't Find The Word</h3>`;
    });
});
function playSound() {
  sound.play();
}
728x90
LIST

'Javascript' 카테고리의 다른 글

Product Filter  (0) 2022.12.04
New Year 2023 Countdown  (0) 2022.12.02
Live Word Counter  (0) 2022.11.30
Search Highlight Text  (0) 2022.11.29
Blob Maker  (0) 2022.11.25