Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Nest.js
- nodejs
- MySQL
- 공룡게임
- Dinosaur
- 자료구조
- Queue
- nestjs
- Bull
- JavaScript
- AWS
- dfs
- 정렬
- MongoDB
- Express
- typeORM
- Python
- class
- mongoose
- flask
- Sequelize
- 게임
- cookie
- OCR
- jest
- GIT
- react
- TypeScript
- game
Archives
- Today
- Total
포시코딩
[IntersectionObserver] 무한 스크롤 (Infinite scroll) 본문
728x90
https://github.com/10-10-gaza/wonbin
GitHub - 10-10-gaza/wonbin
Contribute to 10-10-gaza/wonbin development by creating an account on GitHub.
github.com
[JS] vanilla js로 무한 스크롤(Infinite scroll) 구현하기
🤔무한 스크롤(Infinite scroll)이란? 무한 스크롤은 사용자가 페이지 하단에 도달했을 때, 콘텐츠가 계속 로드되는 사용자 경험(UX) 방식입니다. 한 페이지 아래로 스크롤 하면 끝없이 새로운 화면
eunoia07.tistory.com
위 내용 참고해서 구현한 코드
window.onload = () => {
infiniteScroll();
};
let page = 1;
let isGetOrdersWaitingLoading = false;
const infiniteScroll = () => {
const scrollEnd = document.querySelector('#scroll-end');
const io = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (!entry.isIntersecting) return;
// entry가 interscting 중이 아니라면 함수를 실행하지 않음
if (isGetOrdersWaitingLoading) return;
// 현재 page가 불러오는 중임을 나타내는 flag를 통해 불러오는 중이면 함수를 실행하지 않음
observer.observe(scrollEnd);
getOrdersWaiting(page);
page += 1;
});
});
io.observe(scrollEnd);
};
const getOrdersWaiting = (p) => {
isGetOrdersWaitingLoading = true;
console.log('p: ' + p);
fetch('/api/orders?p='+p, {
method: 'GET',
})
.then(async (res) => {
// ...생략
})
.finally(() => {
isGetOrdersWaitingLoading = false;
});
};
728x90
'JavaScript' 카테고리의 다른 글
[OCR] Tesseract.js (0) | 2023.06.15 |
---|---|
location.href 안먹히는 현상 (0) | 2023.02.05 |
[TypeScript][노마드코더] #4 CLASSES AND INTERFACES(5) - Polymorphism, Generic (0) | 2023.01.22 |
[TypeScript][노마드코더] #4 CLASSES AND INTERFACES(4) (0) | 2023.01.22 |
[TypeScript][노마드코더] #4 CLASSES AND INTERFACES(3) (0) | 2023.01.22 |