Skip to content

Commit ab16fdc

Browse files
committed
feat: add shimmer background when image loads
1 parent bffaf63 commit ab16fdc

File tree

10 files changed

+207
-115
lines changed

10 files changed

+207
-115
lines changed

_includes/refactor-content.html

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,22 @@
8181
{% continue %}
8282
{% endunless %}
8383

84-
{% assign _left = _left | remove: ' /' | replace: ' w=', ' width=' | replace: ' h=', ' height=' %}
85-
{% assign _attrs = _left | split: ' ' %}
84+
{% assign _left = _left | remove: ' /' | replace: ' w=', ' width=' | replace: ' h=', ' height=' %}
85+
{% assign _attrs = _left | split: '" ' %}
8686

8787
{% assign _width = nil %}
8888
{% assign _height = nil %}
8989
{% assign _lqip = nil %}
90+
{% assign _class = nil %}
9091

9192
{% for _attr in _attrs %}
92-
{% assign _pair = _attr | split: '="' %}
93-
{% if _pair.size < 2 %}
93+
{% unless _attr contains '=' %}
9494
{% continue %}
95-
{% endif %}
95+
{% endunless %}
9696

97+
{% assign _pair = _attr | remove: '"' | split: '=' %}
9798
{% capture _key %}{{ _pair | first }}{% endcapture %}
98-
{% capture _value %}{{ _pair | last | remove: '"' }}{% endcapture %}
99+
{% capture _value %}{{ _pair | last }}{% endcapture %}
99100

100101
{% case _key %}
101102
{% when 'width' %}
@@ -106,10 +107,20 @@
106107
{% assign _src = _value %}
107108
{% when 'lqip' %}
108109
{% assign _lqip = _value %}
110+
{% when 'class' %}
111+
{% assign _class = _value %}
109112
{% endcase %}
110113

111114
{% endfor %}
112115

116+
<!-- take out classes -->
117+
{% if _class %}
118+
{% capture _old_class %}class="{{ _class }}"{% endcapture %}
119+
{% assign _left = _left | remove: _old_class %}
120+
{% endif %}
121+
122+
{% assign _final_src = nil %}
123+
113124
{% unless _src contains '//' %}
114125
{% assign _final_src = _path_prefix | append: _src %}
115126
{% capture _src_from %}"{{ _src }}"{% endcapture %}
@@ -127,31 +138,51 @@
127138
{% endif %}
128139

129140
<!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
141+
{% assign _left = _left | replace: 'src=', 'data-src=' %}
130142
{% if _left contains 'class=' %}
131-
{% assign _left = _left | replace: 'class="', 'class="lazyload ' %}
143+
{% assign _left = _left | replace: 'class="', 'class="lazyload '%}
132144
{% else %}
133-
{% assign _left = _left | replace: 'src=', 'class="lazyload" src=' %}
145+
{% assign _left = _left | append: ' class="lazyload"' %}
134146
{% endif %}
135147

136-
{% assign _left = _left | replace: 'src=', 'data-src=' %}
137-
138-
<!-- add placeholder -->
148+
<!-- add image placeholder -->
139149
{% if _lqip %}
140150
{% assign _left = _left | replace: ' lqip=', ' data-lqip="true" src=' %}
141151
{% else %}
142152
{% if _width and _height %}
143-
<!-- Add SVG placehoder to prevent layout reflow -->
153+
<!-- add SVG placehoder -->
144154
{%- capture _svg -%}
145155
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 {{ _width }} {{ _height }}'%3E%3C/svg%3E"
146156
{%- endcapture -%}
147157
{% assign _left = _svg | append: ' ' | append: _left %}
158+
{% assign _class = _class | append: ' shimmer' %}
148159
{% endif %}
149160
{% endif %}
150161

151162
<!-- Bypass the HTML-proofer test -->
152163
{% assign _left = _left | append: ' data-proofer-ignore' %}
153164

154-
<!-- Combine -->
165+
<!-- make sure the `<img>` is wrapped by `<a>` -->
166+
{% assign _parent = _right | slice: 1, 4 %}
167+
168+
{% if _parent == '</a>' %}
169+
<!-- add class to exist <a> tag -->
170+
{% assign _size = _img_content | size | minus: 1 %}
171+
{% capture _class %}
172+
class="img-link{% unless _lqip %} shimmer{% endunless %}"
173+
{% endcapture %}
174+
{% assign _img_content = _img_content | slice: 0, _size | append: _class | append: '>' %}
175+
176+
{% else %}
177+
<!-- create the image wrapper -->
178+
{%- capture _wrapper_start -%}
179+
<a href="{{ _final_src | default: _src }}" class="popup img-link {{ _class }}">
180+
{%- endcapture -%}
181+
{% assign _img_content = _img_content | append: _wrapper_start %}
182+
{% assign _right = _right | prepend: '></a' %}
183+
{% endif %}
184+
185+
<!-- combine -->
155186
{% assign _img_content = _img_content | append: IMG_TAG | append: _left | append: _right %}
156187

157188
{% endfor %}

_javascript/utils/img-extra.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
/**
2-
Set up image popup stuff (https://github.com/dimsemenov/Magnific-Popup)
2+
* Set up image stuff
33
*/
44

5-
$(function () {
6-
const IMG_SCOPE = '#main > div.row:first-child > div:first-child';
7-
8-
if ($(`${IMG_SCOPE} img`).length <= 0) {
5+
(function() {
6+
if ($('#core-wrapper img[data-src]') <= 0) {
97
return;
108
}
119

12-
/* popup */
13-
14-
$(`${IMG_SCOPE} p > img[data-src], ${IMG_SCOPE} img[data-src].preview-img`).each(
15-
function () {
16-
let nextTag = $(this).next();
17-
const title = nextTag.prop('tagName') === 'EM' ? nextTag.text() : '';
18-
const src = $(this).attr('data-src'); // created by lozad.js
19-
20-
$(this).wrap(`<a href="${src}" title="${title}" class="popup"></a>`);
21-
}
22-
);
23-
10+
/* See: <https://github.com/dimsemenov/Magnific-Popup> */
2411
$('.popup').magnificPopup({
2512
type: 'image',
2613
closeOnContentClick: true,
@@ -32,8 +19,10 @@ $(function () {
3219
}
3320
});
3421

35-
/* markup the image links */
36-
37-
$(`${IMG_SCOPE} a`).has('img').addClass('img-link');
22+
/* Stop shimmer when image loaded */
23+
document.addEventListener('lazyloaded', function(e) {
24+
const $img = $(e.target);
25+
$img.parent().removeClass('shimmer');
26+
});
3827

39-
});
28+
})();

_layouts/post.html

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,37 @@ <h1 data-toc-skip>{{ page.title }}</h1>
2727
{% endif %}
2828

2929
{% if page.image %}
30-
<div class="mt-3 mb-3">
31-
<img src="{{ page.image.path | default: page.image }}"
32-
class="preview-img{% if page.image.no_bg %}{{ ' no-bg' }}{% endif %}"
33-
alt="{{ page.image.alt | default: "Preview Image" }}"
30+
{% capture src %}src="{{ page.image.path | default: page.image }}"{% endcapture %}
31+
{% capture class %}class="preview-img{% if page.image.no_bg %}{{ ' no-bg' }}{% endif %}"{% endcapture %}
32+
{% capture alt %}alt="{{ page.image.alt | default: "Preview Image" }}"{% endcapture %}
3433

34+
{% capture w %}
3535
{% if page.image.width %}
3636
width="{{ page.image.width }}"
3737
{% elsif page.image.w %}
3838
width="{{ page.image.w }}"
3939
{% endif %}
40+
{% endcapture %}
4041

42+
{% capture h %}
4143
{% if page.image.height %}
4244
height="{{ page.image.height }}"
4345
{% elsif page.image.h %}
44-
height="{{ page.image.h }}"
46+
h="{{ page.image.h }}"
4547
{% endif %}
48+
{% endcapture %}
4649

50+
{% capture lqip %}
4751
{% if page.image.lqip %}
4852
lqip="{{ page.image.lqip }}"
4953
{% endif %}
54+
{% endcapture %}
5055

51-
><!-- endof img tag -->
52-
53-
{% if page.image.alt %}
56+
<div class="mt-3 mb-3">
57+
<img {{ src }} {{ class }} {{ w | strip }} {{ h | strip }} {{ lqip | strip }} {{ alt }}>
58+
{%- if page.image.alt -%}
5459
<figcaption class="text-center pt-2 pb-2">{{ page.image.alt }}</figcaption>
55-
{% endif %}
60+
{%- endif -%}
5661
</div>
5762
{% endif %}
5863

0 commit comments

Comments
 (0)