Skip to content

Commit b1e0dd8

Browse files
committed
Lazy load thumbnails
1 parent 4abf913 commit b1e0dd8

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

search/search.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ let fpdb = {
22
api: 'https://db-api.unstable.life',
33
images: 'https://infinity.unstable.life/images',
44
resultsPerPage: 100,
5-
thumbnailLimit: 1000,
65
metaMap: {
76
title: "Title",
87
alternateTitles: "Alternate Titles",
@@ -216,18 +215,25 @@ function loadPage(page) {
216215
document.querySelectorAll('.results-current-page').forEach(elem => { elem.textContent = fpdb.currentPage.toLocaleString(); });
217216
document.querySelector('.results').scrollTop = 0;
218217

218+
let logoObserver = new IntersectionObserver(entries => {
219+
entries.forEach(entry => {
220+
if (entry.isIntersecting) {
221+
entry.target.style.backgroundImage = entry.target.getAttribute('lazy-background');
222+
logoObserver.unobserve(entry.target);
223+
}
224+
});
225+
});
226+
219227
for (let i = (page - 1) * fpdb.displayedResults; i < Math.min(fpdb.list.length, page * fpdb.displayedResults); i++) {
220228
let entry = document.createElement('div')
221229
entry.className = 'entry';
222230

223-
if (fpdb.displayedResults < fpdb.thumbnailLimit) {
224-
let logo = document.createElement('div');
225-
logo.className = 'entry-logo';
226-
logo.setAttribute('view', i);
227-
logo.style.backgroundImage = `url("${fpdb.images}/Logos/${fpdb.list[i].id.substring(0, 2)}/${fpdb.list[i].id.substring(2, 4)}/${fpdb.list[i].id}.png?type=jpg")`;
228-
logo.addEventListener('click', loadEntry);
229-
entry.append(logo);
230-
}
231+
let logo = document.createElement('div');
232+
logo.className = 'entry-logo';
233+
logo.setAttribute('view', i);
234+
logo.setAttribute('lazy-background', `url("${fpdb.images}/Logos/${fpdb.list[i].id.substring(0, 2)}/${fpdb.list[i].id.substring(2, 4)}/${fpdb.list[i].id}.png?type=jpg")`);
235+
logo.addEventListener('click', loadEntry);
236+
logoObserver.observe(logo);
231237

232238
let text = document.createElement('div');
233239
text.className = 'entry-text';
@@ -271,7 +277,7 @@ function loadPage(page) {
271277
header.append(title, developer);
272278
subHeader.append(type, tags);
273279
text.append(header, subHeader, description);
274-
entry.append(text);
280+
entry.append(logo, text);
275281
htmlList.append(entry);
276282
}
277283
}

0 commit comments

Comments
 (0)