-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (63 loc) · 2.1 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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}`);
// console.log(res)
const data = await res.json();
// console.log(data);
return data;
}
async function showPosts() {
const posts = await getPosts();
// console.log(posts);
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);
}
window.addEventListener('scroll', () => {
const { scrollTop, scrollHeight, clientHeight } = document.documentElement;
// console.log('ScrollTop: ', scrollTop);
// console.log('ScrollHeight: ', scrollHeight);
// console.log('ClientHeight: ', clientHeight);
// console.log('--------------------------');
if(scrollHeight - scrollTop <= clientHeight) {
showLoading();
}
})
function filterPosts(e) {
const text = e.target.value.toUpperCase();
const posts = document.querySelectorAll('.post');
posts.forEach(post => {
const titlePost = post.querySelector('.post-title').innerText.toUpperCase();
const titleBody = post.querySelector('.post-body').innerText.toUpperCase();
if(titleBody.indexOf(text) > -1 || titlePost.indexOf(text) > -1) {
post.style.display = 'flex';
}
else {
post.style.display = 'none';
}
})
}
showPosts();
filter.addEventListener('input', filterPosts);