๐ช logic
- html ๋ด
input
์ ์์ฑ, ๋ธ๋ก๊ทธ ๋ฆฌ์คํธ๋ js๋ก ๊ตฌ์ฑ ์์ (๊ฐ์ธ๋ div๋ง ์์ฑํด์ฃผ๊ธฐ)
- fetchํจ์๋ก item์ ๋ฐ์์จ๋ค(
getPost()
).
- ๋ฐ์์จ item๋ค์ ๊ฐ๊ฐ htmlํ๊ทธ๋ก ๊ตฌ์ฑํด์(
showPost()
) innerHTMLํด์ค๋ค.
- fetch์ limit์ 5๋ก ์ ํ(5๊ฐ๊น์ง๋ง ํ๋ฉด์ ๋ณด์ด๊ธฐ)ํ๊ณ , page๋ 1๋ก ๊ท์ (1ํ์ด์ง๋ถํฐ)ํ๋ค.
- loading dot์ด ํ์ด์ง๊ฐ ์ถ๊ฐ๋๋ฉด 3์ด ํ์ ๋ณด์ด๋๋ก css ์ ์ฉํ ํจ์ ์์ฑ
๐ html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="index.css" />
<title>My Blog</title>
</head>
<body>
<h1>My Blog</h1>
<div class="filter-container">
<input type="text" id="filter" class="filter" placeholder="Filter posts..." />
</div>
<div id="posts-container">
</div>
<div class="loader">
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
</div>
<script src="index.js"></script>
</body>
</html>
๐ css
@import url("https://fonts.googleapis.com/css?family=Roboto&display=swap");
* {
box-sizing: border-box;
}
body {
background-color: #296ca8;
color: #fff;
font-family: "Roboto", sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
padding-bottom: 100px;
}
h1 {
margin-bottom: 0;
text-align: center;
}
.filter-container {
margin-top: 20px;
width: 80vw;
max-width: 800px;
}
.filter {
width: 100%;
padding: 12px;
font-size: 16px;
}
.post {
position: relative;
background-color: #4992d3;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
border-radius: 3px;
padding: 20px;
margin: 40px 0;
display: flex;
width: 80vw;
max-width: 800px;
}
.post .post-title {
margin: 0;
}
.post .post-body {
margin: 15px 0 0;
line-height: 1.3;
}
.post .post-info {
margin-left: 20px;
}
.post .number {
position: absolute;
top: -15px;
left: -15px;
font-size: 15px;
width: 40px;
height: 40px;
border-radius: 50%;
background: #fff;
color: #296ca8;
display: flex;
align-items: center;
justify-content: center;
padding: 7px 10px;
}
.loader {
opacity: 0;
display: flex;
position: fixed;
bottom: 50px;
transition: opacity 0.3s ease-in;
}
.loader.show {
opacity: 1;
}
.circle {
background-color: #fff;
width: 10px;
height: 10px;
border-radius: 50%;
margin: 5px;
animation: bounce 0.5s ease-in infinite;
}
.circle:nth-of-type(2) {
animation-delay: 0.1s;
}
.circle:nth-of-type(3) {
animation-delay: 0.2s;
}
@keyframes bounce {
0%,
100% {
transform: translateY(0);
}
50% {
transform: translateY(-10px);
}
}
๐ js
"use strict";
const postsContainer = document.getElementById("posts-container");
const loading = document.querySelector(".loader");
const filter = document.getElementById("filter");
let limit = 5;
let page = 1;
async function getPosts() {
const res = await fetch(`https://jsonplaceholder.typicode.com/posts?_limit=${limit}&_page=${page}`);
const data = await res.json();
return data;
}
async function showPosts() {
const posts = await getPosts();
posts.forEach((post) => {
const postEl = document.createElement("div");
postEl.classList.add("post");
postEl.innerHTML = `
<div class="number">${post.id}</div>
<div class="post-info">
<h2 class="post-title">${post.title}</h2>
<p class="post-body">${post.body}</p>
</div>
`;
postsContainer.appendChild(postEl);
});
}
function showLoading() {
loading.classList.add("show");
setTimeout(() => {
loading.classList.remove("show");
setTimeout(() => {
page++;
showPosts();
}, 300);
}, 1000);
}
function filterPosts(e) {
const term = e.target.value.toUpperCase();
const posts = document.querySelectorAll(".post");
posts.forEach((post) => {
const title = post.querySelector(".post-title").innerText.toUpperCase();
const body = post.querySelector(".post-body").innerText.toUpperCase();
if (title.indexOf(term) > -1 || body.indexOf(term) > -1) {
post.style.display = "flex";
} else {
post.style.display = "none";
}
});
}
showPosts();
window.addEventListener("scroll", () => {
const { scrollTop, scrollHeight, clientHeight } = document.documentElement;
if (scrollTop + clientHeight >= scrollHeight - 5) {
showLoading();
}
});
filter.addEventListener("input", filterPosts);