Skip to content

Commit 6084c45

Browse files
authored
Merge pull request #1982 from tomudding/fix/masonry-layout-not-reflowing-after-arsize-change
fix(photo): thumbnails not resizing when reflowing layout
2 parents 81f8367 + 6bc4f63 commit 6084c45

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

public/js/photo.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,47 @@ let Photo = {
99
/*
1010
* Pre size items such that we can do the layouting while the images are loading
1111
*/
12-
var sizer = $('.grid-sizer').width();
13-
var gutter = $('.gutter-sizer').width();
14-
$('figure.pswp-gallery__item > a > img').each(function (index) {
15-
var item = $(this);
16-
var ratio = sizer / item.data('width');
17-
var height = Math.round(ratio * item.data('height'));
18-
if (item.parent().parent().hasClass('potw-thumb')) {
19-
item.attr('width', 2 * sizer + gutter);
20-
item.attr('height', 2 * height + gutter);
21-
} else {
22-
item.attr('width', sizer);
23-
item.attr('height', height);
24-
}
25-
});
12+
function getElementWidth(selector) {
13+
return document.querySelector(selector).offsetWidth;
14+
}
15+
16+
function updateImageSizes() {
17+
let sizer = getElementWidth('.grid-sizer');
18+
let gutter = getElementWidth('.gutter-sizer');
19+
20+
document.querySelectorAll('figure.pswp-gallery__item > a > img').forEach(function (item) {
21+
let ratio = sizer / item.dataset.width;
22+
let height = Math.round(ratio * item.dataset.height);
2623

24+
if (item.closest('.potw-thumb')) {
25+
item.setAttribute('width', 2 * sizer + gutter);
26+
item.setAttribute('height', 2 * height + gutter);
27+
} else {
28+
item.setAttribute('width', sizer);
29+
item.setAttribute('height', height);
30+
}
31+
});
32+
}
33+
34+
updateImageSizes();
2735
let lazyLoadInstance = new LazyLoad({
2836
elements_selector: '.lazy-load',
2937
});
3038

31-
$('.pswp-gallery').masonry({
39+
let msnry = new Masonry('.pswp-gallery', {
3240
itemSelector: '.pswp-gallery__item',
3341
columnWidth: '.grid-sizer',
3442
percentPosition: true,
3543
gutter: '.gutter-sizer',
3644
transitionDuration: 0,
3745
resize: true,
3846
});
47+
48+
// Allow reflowing of the layout when changing the window aspect-ratio and/or size.
49+
window.addEventListener('resize', function () {
50+
updateImageSizes();
51+
msnry.layout();
52+
});
3953
},
4054
initRemoveTag: element => {
4155
element.addEventListener('click', event => {

0 commit comments

Comments
 (0)