Fade thumbnails in over thumbhash on load

This commit is contained in:
Alan Grainger
2026-05-30 11:46:52 +08:00
parent d500418500
commit d545e13fbe
2 changed files with 14 additions and 6 deletions
+9 -6
View File
@@ -108,14 +108,17 @@ html, body {
width: 100%; width: 100%;
height: 100%; height: 100%;
object-fit: cover; object-fit: cover;
opacity: 0;
transition: opacity 0.25s ease-out;
} }
/* Hide the <img> until JS sets its src - otherwise Chrome renders its default /* Fade the thumbnail in once its bytes are decoded (web.js adds .loaded on
broken-image glyph for img elements with explicit dimensions but no src the img's load event). While opacity is 0 the <a>'s thumbhash / gray
(e.g. tiles awaiting load, or tiles whose in-flight fetch was cancelled background shows through, so the blurred thumbhash appears to resolve into
when the user scrolled past). The <a>'s gray background shows through. */ the sharp image rather than snap. Also masks the brief white flash Chrome /
#gallery img:not([src]) { Edge paint between `img.src` being set and the bytes actually rendering. */
visibility: hidden; #gallery img.loaded {
opacity: 1;
} }
#gallery a.thumb-error img { #gallery a.thumb-error img {
+5
View File
@@ -316,6 +316,11 @@ function createTile (index) {
img.alt = '' img.alt = ''
img.dataset.src = item.thumbnailUrl img.dataset.src = item.thumbnailUrl
img.onerror = onThumbError img.onerror = onThumbError
// Mark the image as drawable once its bytes are decoded; the CSS rule
// `#gallery img.loaded` fades opacity from 0 to 1 so the thumbhash blurs
// into the sharp image instead of snapping (and masks Chrome/Edge's brief
// white flash between `img.src` being set and the bytes actually painting).
img.onload = () => img.classList.add('loaded')
a.appendChild(img) a.appendChild(img)
if (item.type === 'VIDEO') { if (item.type === 'VIDEO') {