Skip to content

Commit

Permalink
Add a feature that loads the full image if the thumbnail image doesn'…
Browse files Browse the repository at this point in the history
…t exist.
  • Loading branch information
liaoyanqing666 committed Jan 31, 2025
1 parent 76efdee commit 169c38e
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ function loadThumbnail(index) {
thumbImg.src = `images/thumbs/${index}.jpg`;

thumbImg.onload = function () {
createImageElement(thumbImg, index, resolve);
};

// If the thumbnail image fails to load, try loading the full-size image
thumbImg.onerror = function () {
thumbImg.src = `images/${index}.jpg`;
thumbImg.onload = function () {
createImageElement(thumbImg, index, resolve);
};
thumbImg.onerror = function () {
resolve(null);
};
};

function createImageElement(thumbImg, index, resolve) {
const imgElement = document.createElement('img');
imgElement.dataset.large = `images/${index}.jpg`;
imgElement.src = thumbImg.src;
Expand Down Expand Up @@ -85,11 +100,7 @@ function loadThumbnail(index) {
imgElement.classList.add('thumbnail');

resolve(imgElement);
};

thumbImg.onerror = function () {
resolve(null);
};
}
});
}

Expand Down

0 comments on commit 169c38e

Please sign in to comment.