Skip to content

Commit 1de3d66

Browse files
committed
js: fix lightbox popup image previews
1 parent 259ee5a commit 1de3d66

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

assets/js/_main.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,29 @@ $(document).ready(function () {
9090
});
9191

9292
// add lightbox class to all image links
93-
$("a[href$='.jpg'],a[href$='.jpeg'],a[href$='.JPG'],a[href$='.png'],a[href$='.gif']").addClass("image-popup");
93+
// Add "image-popup" to links ending in image extensions,
94+
// but skip any <a> that already contains an <img>
95+
$("a[href$='.jpg'],\
96+
a[href$='.jpeg'],\
97+
a[href$='.JPG'],\
98+
a[href$='.png'],\
99+
a[href$='.gif'],\
100+
a[href$='.webp']")
101+
.not(':has(img)')
102+
.addClass("image-popup");
103+
104+
// 1) Wrap every <p><img> (except emoji images) in an <a> pointing at the image, and give it the lightbox class
105+
$('p > img').not('.emoji').each(function() {
106+
var $img = $(this);
107+
// skip if it’s already wrapped in an <a.image-popup>
108+
if ( ! $img.parent().is('a.image-popup') ) {
109+
$('<a>')
110+
.addClass('image-popup')
111+
.attr('href', $img.attr('src'))
112+
.insertBefore($img) // place the <a> right before the <img>
113+
.append($img); // move the <img> into the <a>
114+
}
115+
});
94116

95117
// Magnific-Popup options
96118
$(".image-popup").magnificPopup({

0 commit comments

Comments
 (0)