Fixed image lazy loading.

This commit is contained in:
Cotes Chung 2020-05-29 04:51:20 +08:00
parent 171faafc67
commit cfb67a1cb9
3 changed files with 13 additions and 6 deletions

View File

@ -1,7 +1,8 @@
<!-- image lazy load: https://github.com/ApoorvSaxena/lozad.js -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lozad/dist/lozad.min.js"></script>
<script type="text/javascript">
const el = document.querySelectorAll('#post-wrapper img');
const observer = lozad(el);
const imgs = document.querySelectorAll('#post-wrapper img');
const observer = lozad(imgs);
observer.observe();
</script>

View File

@ -64,13 +64,19 @@ layout: default
</div> <!-- .post-meta -->
<div class="post-content">
{% if page.image %}
<img src="{{ page.image }}">
{% endif %}
<!-- Add lozad class into image tags. see: <https://github.com/ApoorvSaxena/lozad.js#usage> -->
{% if content contains '<img src=' %}
{% capture loading %}
{{ "/assets/img/commons/loading.png" | relative_url }}
{% endcapture %}
{% assign replacement = '<img src=' | append: loading | append: ' data-src=' %}
{{ content | replace: '<img src=', replacement }}
{% else %}
{{ content }}
{% endif %}
</div>
<div class="post-tail-wrapper text-muted">

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B