const container = document.querySelector('.container')
const URL = 'https://i.pravatar.cc/500?img='
function getRandNum(){
return Math.floor(Math.random() * 70) // 0~69
}
function loadImages(numImages = 10){
let i=0;
while (i < numImages){
const img = document.createElement('img')
img.src = `${URL}${getRandNum()}`
container.appendChild(img)
i++
}
}
loadImages()
window.innerHeight
=> 브라우저의 현재 높이 (viewport 높이)
document.body.clientHeight
=> 웹페이지 body 높이
window.scrollY
=> 수직으로 스크롤 된 양(높이)
window.scrollY + window.innerHeight
가 document.body.clientHeight
보다 커지면 가장 밑바닥을 지나는 지점이다.
window.addEventListener('scroll', () => {
// console.log(window.scrollY)
// console.log(window.innerHeight)
// if (window.scrollY + window.innerHeight >= document.documentElement.scrollHeight){
if (window.scrollY + window.innerHeight >= document.body.clientHeight){
loadImages()
}
})
좀 더 정확하게 문서 높이 설정하려면, 브라우저 창 사이즈와 스크롤
let scrollHeight = Math.max(
document.body.scrollHeight, document.documentElement.scrollHeight,
document.body.offsetHeight, document.documentElement.offsetHeight,
document.body.clientHeight, document.documentElement.clientHeight
);
[js-toy-pj] infinite scrolling 2
Learn Infinite Scroll in JavaScript
How to implement infinite scrolling in Javascript