์ด์ฌํ infinite scroll์ ๊ตฌํ์ ํ๊ณ ํ
์คํธ๋ฅผ ์งํํ๋๋ฐ...!
๊ฐ๋ฐํ๋ฉด์ ์ด์ํ ํ์์ ๋ฐ๊ฒฌํ์ด์:

$btnBookmark.addEventListener("click", () => {
isBookmarkChecked = !isBookmarkChecked;
if (isBookmarkChecked) {
$btnBookmark.classList.add("active");
$header.classList.add("collapsed");
$header.style.display = "none";
$mainSubTit.style.display = "block";
const data = getMovieListStore;
movieList.setMovies(data, "replace");
} else {
$btnBookmark.classList.remove("active");
$header.classList.remove("collapsed");
$header.style.display = "flex";
$mainSubTit.style.display = "none";
movieList.init();
}
});
export class MovieList {
constructor(selector) {
this.$movieList = document.querySelector(selector);
this.data = [];
this.loading = false;
this.currentPage = 1;
}
init() {
this.loadMovie();
this.setupInfiniteScroll();
}
setupInfiniteScroll() {
window.addEventListener("scroll", () => {
if (this.isNearBottom() && !this.loading && this.data.length < 101) {
this.loadMovie();
}
});
}
setMovies(movies, type = "append") {
if (type === "replace") {
this.data = [];
this.currentPage = 1;
this.data = movies;
} else {
this.data.push(...movies);
}
this.render();
}
}
์ฝ๋๋ฅผ ํ๋ํ๋ ์ดํด๋ณด๋ ์ฌ๋ฌ ๊ฐ์ง ๋ฌธ์ ์ ์ด ์์์ด์:
๋ฌดํ์คํฌ๋กค์ด ๊ณ์ ๋์ ์ค! ๐
loadMovie()๊ฐ ํธ์ถ๋์ด ์ผ๋ฐ ์ํ ๋ชฉ๋ก์ ๋ถ๋ฌ์ด๋ชจ๋ ๊ตฌ๋ถ์ด ์์ ๐ค
์ด๋ฒคํธ ๋ฆฌ์ค๋ ๊ด๋ฆฌ ๋ถ์ฌ โ ๏ธ
class MovieList {
constructor(selector) {
// ... ๊ธฐ์กด ์ฝ๋ ...
this.isBookmarkMode = false; // ๐ ๋ถ๋งํฌ ๋ชจ๋ ์ฒดํฌ ํ๋๊ทธ ์ถ๊ฐ
}
}
class MovieList {
removeInfiniteScroll() {
// ์ด์ ์ ๋ฑ๋ก๋ ์คํฌ๋กค ์ด๋ฒคํธ ์ ๊ฑฐ
window.removeEventListener("scroll", this.handleScroll);
}
setupInfiniteScroll() {
this.removeInfiniteScroll(); // ์ด์ ์ด๋ฒคํธ ์ ๋ฆฌ
this.handleScroll = () => {
// ๋ถ๋งํฌ ๋ชจ๋๊ฐ ์๋ ๋๋ง ์ถ๊ฐ ๋ก๋ฉ
if (this.isNearBottom() && !this.loading && !this.isBookmarkMode) {
this.loadMovie();
}
};
window.addEventListener("scroll", this.handleScroll);
}
}
setBookmarkMode(isBookmark) {
this.isBookmarkMode = isBookmark;
if (isBookmark) {
this.removeInfiniteScroll(); // ๋ถ๋งํฌ ๋ชจ๋๋ฉด ๋ฌดํ์คํฌ๋กค ์ค์ง
} else {
this.setupInfiniteScroll(); // ์ผ๋ฐ ๋ชจ๋๋ฉด ๋ฌดํ์คํฌ๋กค ํ์ฑํ
}
}
// ์๋ชป๋ ๋ฐฉ๋ฒ (์ด๋ฒคํธ ๋ฆฌ์ค๋ ์ค๋ณต ๋ฑ๋ก)
setupInfiniteScroll() {
window.addEventListener("scroll", () => {
// ...
});
}
// ๊ฐ์ ๋ ๋ฐฉ๋ฒ (์ด์ ์ด๋ฒคํธ ์ ๊ฑฐ ํ ๋ฑ๋ก)
setupInfiniteScroll() {
this.removeInfiniteScroll();
this.handleScroll = () => {
// ...
};
window.addEventListener("scroll", this.handleScroll);
}
์ด๋ฒ ํธ๋ฌ๋ธ์ํ ์ ํตํด์ ํ ๊ฐ์ง ๊ธฐ๋ฅ(๋ถ๋งํฌ)์ด ๋ค๋ฅธ ๊ธฐ๋ฅ(๋ฌดํ์คํฌ๋กค)์ ์ํฅ์ ๋ฏธ์น ์ ์๋ค๋ ๊ฑธ ๋ฐฐ์ ์ด์. ํนํ ์ด๋ฒคํธ ๋ฆฌ์ค๋ ๊ด๋ฆฌ์ ์ํ ๊ด๋ฆฌ๊ฐ ์ผ๋ง๋ ์ค์ํ์ง ์ค๊ฐํ๋ค์!
"์ ๋์๊ฐ๋ ๊ฒ ๊ฐ์๋ฐ?" ํ๊ณ ๋์ด๊ฐ๋ค๊ฐ ๋์ค์ ๋ ํฐ ๋ฌธ์ ๊ฐ ๋ ์ ์์๋๋ฐ, ์ด๋ฐ ๊ฒฝํ์ ํตํด ์ข ๋ ๊ผผ๊ผผํ๊ฒ ์ฝ๋๋ฅผ ์์ฑํ๊ฒ ๋ ๊ฒ ๊ฐ์์. ๐