refactor(gallery): calculate flex-basis & flex-grow on generation

Reduce layout shift
This commit is contained in:
Jimmy Cai 2020-12-22 19:24:41 +01:00
parent 69cd9343a0
commit 474d7911e8
No known key found for this signature in database
GPG Key ID: 3EA408E527F37B18
2 changed files with 2 additions and 9 deletions

View File

@ -83,8 +83,7 @@ class StackGallery {
}
/**
* Wrap adjacent figure tags with div.gallery, and append style
* Reference: https://github.com/xieranmaya/blog/issues/6
* Wrap adjacent figure tags with div.gallery
* @param figures
*/
public static wrap(figures: HTMLElement[]) {
@ -97,12 +96,6 @@ class StackGallery {
parentNode.insertBefore(galleryContainer, first)
for (const figure of figures) {
const width = figure.querySelector('img').width,
height = figure.querySelector('img').height;
figure.style.flexGrow = `${width * 100 / height}`;
figure.style.flexBasis = `${width * 240 / height}px`;
galleryContainer.appendChild(figure);
}
}

View File

@ -7,7 +7,7 @@
{{- with $alt -}}
{{- $caption = . | safeHTML -}}
{{- end -}}
<figure>
<figure style="flex-grow: {{ $image.Width * 100 / $image.Height }}; flex-basis: {{ $image.Width * 240 / $image.Height }}">
<a href="{{ $image.RelPermalink }}" data-size="{{ $image.Width }}x{{ $image.Height }}">
<img srcset="{{ $small.RelPermalink }} 480w, {{ $big.RelPermalink }} 1024w"
src="{{ $image.RelPermalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}" loading="lazy"