Skip to content

Commit

Permalink
feat: add shimmer background when image loads
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 committed Dec 24, 2022
1 parent bffaf63 commit ab16fdc
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 115 deletions.
57 changes: 44 additions & 13 deletions _includes/refactor-content.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,22 @@
{% continue %}
{% endunless %}

{% assign _left = _left | remove: ' /' | replace: ' w=', ' width=' | replace: ' h=', ' height=' %}
{% assign _attrs = _left | split: ' ' %}
{% assign _left = _left | remove: ' /' | replace: ' w=', ' width=' | replace: ' h=', ' height=' %}
{% assign _attrs = _left | split: '" ' %}

{% assign _width = nil %}
{% assign _height = nil %}
{% assign _lqip = nil %}
{% assign _class = nil %}

{% for _attr in _attrs %}
{% assign _pair = _attr | split: '="' %}
{% if _pair.size < 2 %}
{% unless _attr contains '=' %}
{% continue %}
{% endif %}
{% endunless %}

{% assign _pair = _attr | remove: '"' | split: '=' %}
{% capture _key %}{{ _pair | first }}{% endcapture %}
{% capture _value %}{{ _pair | last | remove: '"' }}{% endcapture %}
{% capture _value %}{{ _pair | last }}{% endcapture %}

{% case _key %}
{% when 'width' %}
Expand All @@ -106,10 +107,20 @@
{% assign _src = _value %}
{% when 'lqip' %}
{% assign _lqip = _value %}
{% when 'class' %}
{% assign _class = _value %}
{% endcase %}

{% endfor %}

<!-- take out classes -->
{% if _class %}
{% capture _old_class %}class="{{ _class }}"{% endcapture %}
{% assign _left = _left | remove: _old_class %}
{% endif %}

{% assign _final_src = nil %}

{% unless _src contains '//' %}
{% assign _final_src = _path_prefix | append: _src %}
{% capture _src_from %}"{{ _src }}"{% endcapture %}
Expand All @@ -127,31 +138,51 @@
{% endif %}

<!-- lazy-load images <https://github.com/aFarkas/lazysizes#readme> -->
{% assign _left = _left | replace: 'src=', 'data-src=' %}
{% if _left contains 'class=' %}
{% assign _left = _left | replace: 'class="', 'class="lazyload ' %}
{% assign _left = _left | replace: 'class="', 'class="lazyload '%}
{% else %}
{% assign _left = _left | replace: 'src=', 'class="lazyload" src=' %}
{% assign _left = _left | append: ' class="lazyload"' %}
{% endif %}

{% assign _left = _left | replace: 'src=', 'data-src=' %}
<!-- add placeholder -->
<!-- add image placeholder -->
{% if _lqip %}
{% assign _left = _left | replace: ' lqip=', ' data-lqip="true" src=' %}
{% else %}
{% if _width and _height %}
<!-- Add SVG placehoder to prevent layout reflow -->
<!-- add SVG placehoder -->
{%- capture _svg -%}
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 {{ _width }} {{ _height }}'%3E%3C/svg%3E"
{%- endcapture -%}
{% assign _left = _svg | append: ' ' | append: _left %}
{% assign _class = _class | append: ' shimmer' %}
{% endif %}
{% endif %}

<!-- Bypass the HTML-proofer test -->
{% assign _left = _left | append: ' data-proofer-ignore' %}

<!-- Combine -->
<!-- make sure the `<img>` is wrapped by `<a>` -->
{% assign _parent = _right | slice: 1, 4 %}

{% if _parent == '</a>' %}
<!-- add class to exist <a> tag -->
{% assign _size = _img_content | size | minus: 1 %}
{% capture _class %}
class="img-link{% unless _lqip %} shimmer{% endunless %}"
{% endcapture %}
{% assign _img_content = _img_content | slice: 0, _size | append: _class | append: '>' %}

{% else %}
<!-- create the image wrapper -->
{%- capture _wrapper_start -%}
<a href="{{ _final_src | default: _src }}" class="popup img-link {{ _class }}">
{%- endcapture -%}
{% assign _img_content = _img_content | append: _wrapper_start %}
{% assign _right = _right | prepend: '></a' %}
{% endif %}

<!-- combine -->
{% assign _img_content = _img_content | append: IMG_TAG | append: _left | append: _right %}

{% endfor %}
Expand Down
31 changes: 10 additions & 21 deletions _javascript/utils/img-extra.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
/**
Set up image popup stuff (https://github.com/dimsemenov/Magnific-Popup)
* Set up image stuff
*/

$(function () {
const IMG_SCOPE = '#main > div.row:first-child > div:first-child';

if ($(`${IMG_SCOPE} img`).length <= 0) {
(function() {
if ($('#core-wrapper img[data-src]') <= 0) {
return;
}

/* popup */

$(`${IMG_SCOPE} p > img[data-src], ${IMG_SCOPE} img[data-src].preview-img`).each(
function () {
let nextTag = $(this).next();
const title = nextTag.prop('tagName') === 'EM' ? nextTag.text() : '';
const src = $(this).attr('data-src'); // created by lozad.js

$(this).wrap(`<a href="${src}" title="${title}" class="popup"></a>`);
}
);

/* See: <https://github.com/dimsemenov/Magnific-Popup> */
$('.popup').magnificPopup({
type: 'image',
closeOnContentClick: true,
Expand All @@ -32,8 +19,10 @@ $(function () {
}
});

/* markup the image links */

$(`${IMG_SCOPE} a`).has('img').addClass('img-link');
/* Stop shimmer when image loaded */
document.addEventListener('lazyloaded', function(e) {
const $img = $(e.target);
$img.parent().removeClass('shimmer');
});

});
})();
23 changes: 14 additions & 9 deletions _layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,37 @@ <h1 data-toc-skip>{{ page.title }}</h1>
{% endif %}

{% if page.image %}
<div class="mt-3 mb-3">
<img src="{{ page.image.path | default: page.image }}"
class="preview-img{% if page.image.no_bg %}{{ ' no-bg' }}{% endif %}"
alt="{{ page.image.alt | default: "Preview Image" }}"
{% capture src %}src="{{ page.image.path | default: page.image }}"{% endcapture %}
{% capture class %}class="preview-img{% if page.image.no_bg %}{{ ' no-bg' }}{% endif %}"{% endcapture %}
{% capture alt %}alt="{{ page.image.alt | default: "Preview Image" }}"{% endcapture %}

{% capture w %}
{% if page.image.width %}
width="{{ page.image.width }}"
{% elsif page.image.w %}
width="{{ page.image.w }}"
{% endif %}
{% endcapture %}

{% capture h %}
{% if page.image.height %}
height="{{ page.image.height }}"
{% elsif page.image.h %}
height="{{ page.image.h }}"
h="{{ page.image.h }}"
{% endif %}
{% endcapture %}

{% capture lqip %}
{% if page.image.lqip %}
lqip="{{ page.image.lqip }}"
{% endif %}
{% endcapture %}

><!-- endof img tag -->

{% if page.image.alt %}
<div class="mt-3 mb-3">
<img {{ src }} {{ class }} {{ w | strip }} {{ h | strip }} {{ lqip | strip }} {{ alt }}>
{%- if page.image.alt -%}
<figcaption class="text-center pt-2 pb-2">{{ page.image.alt }}</figcaption>
{% endif %}
{%- endif -%}
</div>
{% endif %}

Expand Down
Loading

0 comments on commit ab16fdc

Please sign in to comment.