-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
32 lines (25 loc) · 999 Bytes
/
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
document.addEventListener("contextmenu", function(e){
e.preventDefault();
}, false);
/*
Infinity Scrolling using Vanilla JavaScript's build in method element.getBoundingClientRect() .
element.getBoundingClientRect() is used to know the current possition of an element
*/
const wrapper = document.querySelector('.wrapper')
let postId = 1
document.addEventListener('scroll', async function (){
if(!touchLastItem(getElements()[getElements().length - 1])) return
const res = await fetch(`https://jsonplaceholder.typicode.com/comments?postId=${postId}`)
const result = await res.json()
for (let i = 0; i < result.length; i++) {
wrapper.innerHTML += `<h1>${result[i].name}</h1>`
}
postId++
})
function getElements() {
return document.querySelectorAll('h1')
}
function touchLastItem(el) {
var rect = el.getBoundingClientRect()
return rect.bottom > 0 && rect.top < (window.innerHeight || document.documentElement.clientHeight)
}