-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathordnung.js
99 lines (81 loc) · 2.89 KB
/
ordnung.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
93
94
95
96
97
98
99
// Ordnung.js
'use strict';
(function(window) {
var container,
vendorPrefixes = [
'-webkit-transform',
'-moz-transform',
'-o-transform',
'transform'
];
// Init
var Ordnung = function(_container) {
container = _container;
};
Ordnung.prototype.layout = function() {
var elems = container.children,
shiftBuffer = [],
sumOff = {},
lastRect = 0;
// Get element dimensions
var offsetBuff = Array.prototype.slice.call(elems).map(function(e) {
return e.getBoundingClientRect()
});
// Main loop
for (var i = 0, l = elems.length-1; i <= l; i++) {
var rect = offsetBuff[i], xOffset = 0, yOffset = 0,
fromTop = rect.top + window.pageYOffset;
// Init column buffer
if (!(rect.left in sumOff)) {
sumOff[rect.left] = fromTop;
}
// Column state
var col = sumOff[rect.left];
// Current Top offset minus sum of moves
yOffset = yOffset ? yOffset : fromTop - col;
// Shift horizontally?
var lastOff = sumOff[lastRect];
if (lastRect != rect.left && lastOff > 0 && lastOff < col) {
yOffset = fromTop - sumOff[lastRect];
xOffset = lastRect - rect.left;
}
// Defer HTML layout
shiftBuffer.push(("translateY(-" + yOffset + "px)") +
(xOffset ? "translateX(" + xOffset + "px)" : ""))
// Save the last dance
if (!xOffset) lastRect = rect.left;
var xCol = xOffset ? lastRect : rect.left;
sumOff[xCol] = sumOff[xCol] + rect.height;
}
// Do transforms
for (i = 0; i <= l; i++) {
vendorPrefixes.forEach(function(prefix) {
elems[i].style[prefix] = shiftBuffer[i];
})
}
// Total height
var height = [];
for (var key in sumOff) {
height.push(sumOff[key])
};
// Adjust container
var totalHeight = Math.max.apply(Math, height) - container.offsetTop;
container.style.height = (totalHeight - window.pageYOffset) + "px";
};
// Expose to AMD environment, e.g. RequireJS
if (typeof define === 'function' && define.amd) {
define(function() {
return Ordnung;
});
return;
}
// Expose to NodeJS
if ('undefined' !== typeof module && module.exports) {
module.exports = Ordnung;
return;
}
// Expose to window if unknown
window.Ordnung = window.Ordnung || Ordnung;
// This line either passes the `window` as an argument or
// an empty Object-literal if `window` is not defined.
}(('undefined' !== typeof window) ? window : {}));