Skip to content

Commit e8c7357

Browse files
committed
Lazy load the image of posts.
1 parent 91caad9 commit e8c7357

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

_includes/head.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js"
6565
integrity="sha256-5+02zu5UULQkO7w1GIr6vftCgMfFdZcAHeDtFnKZsBs=" crossorigin="anonymous"></script>
6666

67+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lozad/dist/lozad.min.js"></script>
68+
6769
<script src="{{ site.baseurl }}/assets/js/dist/commons.js" async></script>
6870

6971
{% if jekyll.environment == 'production' %}

_layouts/post.html

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,21 @@ <h1 data-toc-skip>{{ page.title }}</h1>
4242
{% endif %}
4343
</div> <!-- .post-meta -->
4444

45+
{% capture post_content %}
4546
<div class="post-content">
4647
{% if page.image %}
4748
<img src="{{ page.image }}" class="img-rounded">
4849
{% endif %}
4950
{{ content }}
50-
</div><!-- .post-content -->
51+
</div>
52+
{% endcapture %}
53+
54+
<!-- Add lozad class into image tags. see: <https://github.com/ApoorvSaxena/lozad.js#usage> -->
55+
{% if post_content contains '<img src=' %}
56+
{{ post_content | replace: '<img src=', '<img class="lozad" data-src=' }}
57+
{% else %}
58+
{{ post_content }}
59+
{% endif %}
5160
5261
<div class="post-tail text-muted">
5362
<!-- Tags -->
@@ -97,3 +106,9 @@ <h1 data-toc-skip>{{ page.title }}</h1>
97106
</div> <!-- div.col-12..col-lg-11.col-xl-8 -->
98107

99108
{% include panel.html %}
109+
110+
<!-- image lazy load -->
111+
<script type="text/javascript">
112+
const observer = lozad();
113+
observer.observe();
114+
</script>

0 commit comments

Comments
 (0)