Skip to content

Commit 84bb4d0

Browse files
committed
Add image popup effect
1 parent e8296fe commit 84bb4d0

File tree

15 files changed

+122
-72
lines changed

15 files changed

+122
-72
lines changed

_includes/css-selector.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@
88
<link rel="stylesheet"
99
href="https://cdn.jsdelivr.net/gh/afeld/[email protected]/dist/bootstrap-toc.min.css">
1010
{% endif %}
11+
12+
{% if page.layout == 'page' or page.layout == 'post' %}
13+
<!-- Manific Popup -->
14+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/magnific-popup.min.css">
15+
{% endif %}

_includes/js-selector.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@
1010
{% endif %}
1111
{% endif %}
1212

13+
{% if page.layout == 'post' or page.layout == 'page' %}
14+
<!-- image lazy-loading & popup -->
15+
<script async
16+
src="https://cdn.jsdelivr.net/combine/npm/lozad/dist/lozad.min.js,npm/magnific-popup@1/dist/jquery.magnific-popup.min.js"></script>
17+
{% endif %}
18+
1319
{% if page.layout == 'home'
20+
or page.layout == 'categories'
1421
or page.layout == 'post'
15-
or page.layout == 'categories' %}
22+
or page.layout == 'page' %}
1623
{% assign type = page.layout %}
1724
{% else %}
18-
{% assign type = "page" %}
25+
{% assign type = "commons" %}
1926
{% endif %}
2027

2128
{% assign js = type | prepend: '/assets/js/dist/' | append: '.min.js' %}

_includes/lozad.html

Lines changed: 0 additions & 10 deletions
This file was deleted.

_includes/refactor-content.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
{% assign _content = _content | replace: '<img src="/', img_path_replacement %}
4747

4848
<!-- lazy-load images <https://github.com/ApoorvSaxena/lozad.js#usage> -->
49-
{% assign lozad = true %}
5049
{% assign img_placehodler
5150
= 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' %}
5251

@@ -60,7 +59,3 @@
6059

6160
<!-- return -->
6261
{{ _content }}
63-
64-
{% if lozad %}
65-
{% include lozad.html %}
66-
{% endif %}

_javascript/commons/img-link.js

Lines changed: 0 additions & 8 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

_javascript/utils/img-extra.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
Lazy load images (https://github.com/ApoorvSaxena/lozad.js)
3+
and popup when clicked (https://github.com/dimsemenov/Magnific-Popup)
4+
*/
5+
6+
$(function() {
7+
8+
const IMG_SCOPE = '#main > div.row:first-child > div:first-child';
9+
10+
if ($(`${IMG_SCOPE} img`).length <= 0 ) {
11+
return;
12+
}
13+
14+
/* lozy loading */
15+
16+
const imgs = document.querySelectorAll(`${IMG_SCOPE} img[data-src]`);
17+
const observer = lozad(imgs);
18+
observer.observe();
19+
20+
/* popup */
21+
22+
$(`${IMG_SCOPE} p > img[data-src],${IMG_SCOPE} img[data-src].preview-img`).each(
23+
function() {
24+
let nextTag = $(this).next();
25+
const title = nextTag.prop('tagName') === 'EM' ? nextTag.text() : '';
26+
const src = $(this).attr('data-src'); // created by lozad.js
27+
28+
$(this).wrap(`<a href="${src}" title="${title}" class="popup"></a>`);
29+
}
30+
);
31+
32+
$('.popup').magnificPopup({
33+
type: 'image',
34+
closeOnContentClick: true,
35+
showCloseBtn: false,
36+
zoom: {
37+
enabled: true,
38+
duration: 300,
39+
easing: 'ease-in-out'
40+
}
41+
});
42+
43+
44+
/* markup the image links */
45+
$(`${IMG_SCOPE} a`).has("img").addClass('img-link');
46+
47+
});
48+

_sass/addon/commons.scss

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -363,44 +363,24 @@ footer {
363363
overflow-wrap: break-word;
364364
word-wrap: break-word;
365365

366-
@mixin img-caption {
367-
+ em {
368-
display: block;
369-
text-align: center;
370-
font-style: normal;
371-
font-size: 80%;
372-
padding: 0;
373-
color: #6d6c6c;
374-
}
375-
}
376-
377-
@mixin img-style($caption: false) {
378-
379-
img[data-src] {
380-
margin: 0.5rem 0;
381-
382-
@if $caption {
383-
@include img-caption;
384-
}
385-
386-
&.left {
387-
float: left;
388-
margin: 0.75rem 1rem 1rem 0;
389-
}
366+
img[data-src] {
367+
margin: 0.5rem 0;
390368

391-
&.right {
392-
float: right;
393-
margin: 0.75rem 0 1rem 1rem;
394-
}
369+
&.left {
370+
float: left;
371+
margin: 0.75rem 1rem 1rem 0;
372+
}
395373

396-
&.shadow {
397-
filter: drop-shadow(2px 4px 6px rgba(0, 0, 0, 0.08));
398-
}
374+
&.right {
375+
float: right;
376+
margin: 0.75rem 0 1rem 1rem;
377+
}
399378

379+
&.shadow {
380+
filter: drop-shadow(2px 4px 6px rgba(0, 0, 0, 0.08));
400381
}
401-
}
402382

403-
@include img-style(true);
383+
}
404384

405385
a {
406386
blockquote & {
@@ -415,17 +395,27 @@ footer {
415395
}
416396
}
417397

418-
&.img-link {
419-
@include img-style;
420-
@include img-caption;
398+
&.popup { // created by `_includes/img-extra.html`
399+
cursor: zoom-in;
400+
401+
> img[data-src]:not(.normal):not(.left):not(.right) {
402+
@include align-center;
403+
}
421404
}
422-
}
423405

424-
> p {
425-
> img[data-src]:not(.normal):not(.left):not(.right) {
426-
@include align-center;
406+
&.img-link {
407+
+ em {
408+
display: block;
409+
text-align: center;
410+
font-style: normal;
411+
font-size: 80%;
412+
padding: 0;
413+
color: #6d6c6c;
414+
}
427415
}
428-
}
416+
417+
} // a
418+
429419

430420
ul {
431421
// attribute 'hide-bullet' was added by liquid
@@ -584,6 +574,14 @@ footer {
584574
transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out;
585575
}
586576

577+
/* --- override magnific-popup --- */
578+
579+
figure .mfp-title {
580+
text-align: center;
581+
padding-right: 0;
582+
margin-top: 0.5rem;
583+
}
584+
587585
/* --- sidebar layout --- */
588586

589587
$tab-count: 5 !default;

assets/js/dist/categories.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)