|
1 | 1 | const { imageUrls, totalColumns, totalRows, imageNames, imageSizes } = compareData; |
2 | 2 | let absoluteIndex = 0; |
3 | 3 |
|
| 4 | +// Replace the existing preloadImages function with this improved version |
| 5 | +function preloadImages() { |
| 6 | + // Create a promise for each image load |
| 7 | + const preloadPromises = imageUrls.map(url => { |
| 8 | + return new Promise((resolve, reject) => { |
| 9 | + const img = new Image(); |
| 10 | + img.onload = () => resolve(url); |
| 11 | + img.onerror = () => reject(url); |
| 12 | + img.src = `/uploads/${url}`; |
| 13 | + }); |
| 14 | + }); |
| 15 | + |
| 16 | + // Add loading indicator to the page |
| 17 | + const loadingIndicator = document.createElement('div'); |
| 18 | + loadingIndicator.id = 'preloadIndicator'; |
| 19 | + loadingIndicator.style.cssText = 'position: fixed; bottom: 20px; right: 20px; background: rgba(0,0,0,0.7); color: white; padding: 10px; border-radius: 5px;'; |
| 20 | + loadingIndicator.textContent = 'Loading images...'; |
| 21 | + document.body.appendChild(loadingIndicator); |
| 22 | + |
| 23 | + // Wait for all images to load |
| 24 | + Promise.allSettled(preloadPromises) |
| 25 | + .then(results => { |
| 26 | + const loaded = results.filter(r => r.status === 'fulfilled').length; |
| 27 | + const failed = results.filter(r => r.status === 'rejected').length; |
| 28 | + console.log(`Preloaded ${loaded} images, ${failed} failed`); |
| 29 | + loadingIndicator.remove(); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
4 | 33 | // Get both desktop and mobile elements |
5 | 34 | const currentImage = document.getElementById('currentImage'); |
6 | 35 | const currentImageInfoSpan = document.getElementById('currentImageInfo'); |
@@ -451,6 +480,10 @@ updateDisplay(); |
451 | 480 | updateNavigation(); |
452 | 481 |
|
453 | 482 | document.addEventListener('DOMContentLoaded', () => { |
| 483 | + // Start preloading images |
| 484 | + preloadImages(); |
| 485 | + |
| 486 | + // Check for saved preferences |
454 | 487 | const savedViewMode = getCookie('imageViewMode'); |
455 | 488 | if (savedViewMode === 'original') { |
456 | 489 | currentImage.classList.remove('fit'); |
|
0 commit comments