(img)
| 85 | |
| 86 | // 加载图片 (with error handling) |
| 87 | loadImage(img) { |
| 88 | const src = img.dataset.src; |
| 89 | if (!src) return; |
| 90 | |
| 91 | // Create a new image to preload |
| 92 | const tempImg = new Image(); |
| 93 | |
| 94 | tempImg.onload = () => { |
| 95 | // Check WebP support |
| 96 | if (this.supportsWebP()) { |
| 97 | img.src = src.replace(/\.(jpg|jpeg|png)$/i, '.webp'); |
| 98 | } else { |
| 99 | img.src = src; |
| 100 | } |
| 101 | img.classList.add('fade-in'); |
| 102 | img.removeAttribute('data-src'); |
| 103 | }; |
| 104 | |
| 105 | tempImg.onerror = () => { |
| 106 | // Fallback to original format if WebP fails |
| 107 | img.src = src; |
| 108 | img.classList.add('fade-in'); |
| 109 | img.removeAttribute('data-src'); |
| 110 | }; |
| 111 | |
| 112 | // Trigger preload |
| 113 | tempImg.src = this.supportsWebP() ? src.replace(/\.(jpg|jpeg|png)$/i, '.webp') : src; |
| 114 | } |
| 115 | |
| 116 | // 检查WebP支持(缓存结果避免重复创建canvas) |
| 117 | supportsWebP() { |
no test coverage detected