From 6d1d440c00fce06aa83f00c3bbc374ac429e44ce Mon Sep 17 00:00:00 2001
From: Cotes Chung <11371340+cotes2020@users.noreply.github.com>
Date: Wed, 30 Jun 2021 19:08:42 +0800
Subject: [PATCH] Preventing image reflow (fix #351)
---
_includes/refactor-content.html | 51 +++++++++++++++++++++++++++++----
_javascript/utils/img-extra.js | 9 +++---
_layouts/post.html | 6 +++-
_sass/addon/commons.scss | 1 +
4 files changed, 55 insertions(+), 12 deletions(-)
diff --git a/_includes/refactor-content.html b/_includes/refactor-content.html
index 973dfb36b..c9d7c0256 100644
--- a/_includes/refactor-content.html
+++ b/_includes/refactor-content.html
@@ -35,8 +35,10 @@
%}
{% endif %}
+
{% if _content contains '' | first | split: ' ' %}
+
+ {% for _attr in _attrs %}
+ {% capture _key %}{{ _attr | split: '=' | first }}{% endcapture %}
+ {% capture _value %}{{ _attr | split: '=' | last | replace: '"', '' }}{% endcapture %}
+
+ {% case _key %}
+ {% when 'width' %}
+ {% assign _width = _value %}
+ {% when 'height' %}
+ {% assign _height = _value %}
+ {% endcase %}
+
+ {% if _width and _height %}
+ {% capture _svg %}data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 {{_width}} {{_height}}'%3E%3C/svg%3E{% endcapture %}
+ {% assign _img_content = _img_content | append: '
{{ _content }}
diff --git a/_javascript/utils/img-extra.js b/_javascript/utils/img-extra.js
index bdc60e0c2..91e732480 100644
--- a/_javascript/utils/img-extra.js
+++ b/_javascript/utils/img-extra.js
@@ -11,10 +11,10 @@ $(function() {
return;
}
- /* lozy loading */
+ /* lazy loading */
- const imgs = document.querySelectorAll(`${IMG_SCOPE} img[data-src]`);
- const observer = lozad(imgs);
+ const imgList = document.querySelectorAll(`${IMG_SCOPE} img[data-src]`);
+ const observer = lozad(imgList);
observer.observe();
/* popup */
@@ -40,9 +40,8 @@ $(function() {
}
});
-
/* markup the image links */
+
$(`${IMG_SCOPE} a`).has("img").addClass('img-link');
});
-
diff --git a/_layouts/post.html b/_layouts/post.html
index d95aec671..2cea2c66d 100644
--- a/_layouts/post.html
+++ b/_layouts/post.html
@@ -44,7 +44,11 @@ layout: default