Skip to content

Commit 09d109f

Browse files
committed
Fixed image lazy loading.
1 parent 5b54436 commit 09d109f

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

_includes/lozad.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<!-- image lazy load: https://github.com/ApoorvSaxena/lozad.js -->
22
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lozad/dist/lozad.min.js"></script>
3+
34
<script type="text/javascript">
4-
const el = document.querySelectorAll('#post-wrapper img');
5-
const observer = lozad(el);
5+
const imgs = document.querySelectorAll('#post-wrapper img');
6+
const observer = lozad(imgs);
67
observer.observe();
78
</script>

_layouts/post.html

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,19 @@ <h1 data-toc-skip>{{ page.title }}</h1>
6464
</div> <!-- .post-meta -->
6565

6666
<div class="post-content">
67-
6867
{% if page.image %}
6968
<img src="{{ page.image }}">
7069
{% endif %}
71-
72-
{{ content }}
73-
70+
<!-- Add lozad class into image tags. see: <https://github.com/ApoorvSaxena/lozad.js#usage> -->
71+
{% if content contains '<img src=' %}
72+
{% capture loading %}
73+
{{ "/assets/img/commons/loading.png" | relative_url }}
74+
{% endcapture %}
75+
{% assign replacement = '<img src=' | append: loading | append: ' data-src=' %}
76+
{{ content | replace: '<img src=', replacement }}
77+
{% else %}
78+
{{ content }}
79+
{% endif %}
7480
</div>
7581
7682
<div class="post-tail-wrapper text-muted">

assets/img/commons/loading.png

99 Bytes
Loading

0 commit comments

Comments
 (0)