상세 페이지 레이지 로딩 구현

This commit is contained in:
ByungyeonKim
2021-10-26 18:10:16 +09:00
parent b709d6e48d
commit c8bcd179fb
2 changed files with 25 additions and 5 deletions

View File

@@ -38,14 +38,34 @@ const goToDetailPage = () => {
.detailPage(url)
.then((result) => {
store.setSelectedPage({ selectedPage: result });
console.log(store);
render();
useLazyLoading();
});
selectedPage = true;
});
});
};
const useLazyLoading = () => {
const imgs = document.querySelectorAll('.article-body img');
const observerCallback = (entries, observer) => {
entries.forEach(({ isIntersecting, intersectionRatio, target }) => {
console.log(target);
if (isIntersecting && intersectionRatio > 0) {
if (target.dataset.src) {
target.src = target.dataset.src;
}
observer.unobserve(target);
}
});
};
const io = new IntersectionObserver(observerCallback);
imgs.forEach((img) => io.observe(img));
};
const render = () => {
const app = document.querySelector('#app');
app.innerHTML = `

View File

@@ -1,11 +1,11 @@
import store from '../store.js';
const Detail = () => `
<div class="article-title">
<h2 class="main-title">${store.state.selectedPage.title}</h2>
<span class="author">by ${store.state.selectedPage.mediaName}</span>
</div>
<section class="contents-section">
<div class="article-title">
<h2 class="main-title">${store.state.selectedPage.title}</h2>
<span class="author">${store.state.selectedPage.mediaName}</span>
</div>
<article class="article-body">${store.state.selectedPage.mainContents}</article>
</section>
`;