-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasonry.js
92 lines (74 loc) · 2.35 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import "./style.scss";
function debounce(fn, timeInMs) {
let timeFrame;
return function () {
clearTimeout(timeFrame);
timeFrame = setTimeout(fn, timeInMs);
};
}
class Masonry {
constructor(selector) {
this._grid = document.querySelector(selector);
this._listeners();
setTimeout(this._buildLayout.bind(this), 100);
}
get _columns() {
return getComputedStyle(this._grid).gridTemplateColumns.split(" ");
}
get _rows() {
return getComputedStyle(this._grid).gridTemplateRows.split(" ");
}
_listeners() {
window.addEventListener(
"resize",
debounce(this._buildLayout.bind(this), 50)
);
}
_getAboveRowHeight(cellIdx) {
const rowIdx =
Math.floor((cellIdx / this._columns.length) % this._rows.length) - 1;
return parseFloat(this._rows[rowIdx].replace("px", ""));
}
_calculateMarginTop(cells, cellIdx) {
const aboveCell = cells[cellIdx - this._columns.length];
if (!aboveCell) {
return null;
}
const aboveRowHeight = this._getAboveRowHeight(cellIdx);
const aboveCellHeight = aboveCell.clientHeight;
const aboveCellYTranslate = this._getTranslateYValues(aboveCell);
return aboveRowHeight - aboveCellHeight - aboveCellYTranslate;
}
_getTranslateYValues(cell) {
const yPos = /translateY\((.*)px\)/i;
const translateAmount = cell.style.transform.match(yPos) || 0;
return parseInt(translateAmount[1]);
}
_getBelowCellsInColumn(cells, cellIdx) {
const subGrid = cells.slice(cellIdx);
return subGrid.filter((_, idx) => idx % this._columns.length === 0);
}
_applyStyles(cell, availableMargin) {
const yPos = /translateY\((.*)px\)/i;
cell.style.transform = cell.style.transform.replace(
yPos,
(_, $2) => `translateY(${$2 - availableMargin}px)`
);
}
_buildLayout() {
const cells = Array.from(this._grid.children);
cells.forEach((cell, idx) => {
/** Force cell to be as height as the content it wraps around */
cell.style.height = "max-content";
/** Reset any previous style updates */
cell.style.transform = "translateY(0px)";
const availableMargin = this._calculateMarginTop(cells, idx);
if (!availableMargin) {
return;
}
this._getBelowCellsInColumn(cells, idx).forEach((_cell) => {
this._applyStyles(_cell, availableMargin);
});
});
}
}