-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasonry.js
35 lines (33 loc) · 1.05 KB
/
masonry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"use strict";
let masonry = {
/**
* Initial masonry resize, add listeners
*/
init: function() {
this.resize();
window.addEventListener('load', this.resize);
window.addEventListener('resize', this.resize);
window.addEventListener('orientationChange', this.resize);
},
/**
* For each masonry root, update its content with the appropriate number of
* rows
*/
resize: function() {
document.querySelectorAll('.masonry').forEach(root => {
let rootStyle = window.getComputedStyle(root);
let rowGap = parseInt(rootStyle.gridRowGap, 10);
let rowHeight = parseInt(rootStyle.gridAutoRows, 10);
for (let i = 0; i < root.children.length; i++) {
let child = root.children[i];
let contentHeight = child.children[0].offsetHeight
let rows = Math.ceil((contentHeight + rowGap) / (rowHeight + rowGap));
child.style.gridRowEnd = 'span ' + rows;
}
});
}
};
if (document.readyState == 'Loading')
window.addEventListener('DOMContentLoaded', masonry.init);
else
masonry.init();