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 --> <!-- 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" src="https://cdn.jsdelivr.net/npm/lozad/dist/lozad.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">
const el = document.querySelectorAll('#post-wrapper img'); const imgs = document.querySelectorAll('#post-wrapper img');
const observer = lozad(el); const observer = lozad(imgs);
observer.observe(); observer.observe();
</script> </script>

View file

@ -64,13 +64,19 @@ layout: default
</div> <!-- .post-meta --> </div> <!-- .post-meta -->
<div class="post-content"> <div class="post-content">
{% if page.image %} {% if page.image %}
<img src="{{ page.image }}"> <img src="{{ page.image }}">
{% endif %} {% 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 }} {{ content }}
{% endif %}
</div> </div>
<div class="post-tail-wrapper text-muted"> <div class="post-tail-wrapper text-muted">

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B