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
| function waterfall(a) { function b(a, b) { var c = window.getComputedStyle(b); return parseFloat(c["margin" + a]) || 0 }
function c(a) { return a + "px" }
function d(a) { return parseFloat(a.style.top) }
function e(a) { return parseFloat(a.style.left) }
function f(a) { return a.clientWidth }
function g(a) { return a.clientHeight }
function h(a) { return d(a) + g(a) + b("Bottom", a) }
function i(a) { return e(a) + f(a) + b("Right", a) }
function j(a) { a = a.sort(function(a, b) { return h(a) === h(b) ? e(b) - e(a) : h(b) - h(a) }) }
function k(b) { f(a) != t && (b.target.removeEventListener(b.type, arguments.callee), waterfall(a)) } "string" == typeof a && (a = document.querySelector(a)); var l = [].map.call(a.children, function(a) { return a.style.position = "absolute", a }); a.style.position = "relative"; var m = []; l.length && (l[0].style.top = "0px", l[0].style.left = c(b("Left", l[0])), m.push(l[0])); for (var n = 1; n < l.length; n++) { var o = l[n - 1], p = l[n], q = i(o) + f(p) <= f(a); if (!q) break; p.style.top = o.style.top, p.style.left = c(i(o) + b("Left", p)), m.push(p) } for (; n < l.length; n++) { j(m); var p = l[n], r = m.pop(); p.style.top = c(h(r) + b("Top", p)), p.style.left = c(e(r)), m.push(p) } j(m); var s = m[0]; a.style.height = c(h(s) + b("Bottom", s)); var t = f(a); window.addEventListener ? window.addEventListener("resize", k) : document.body.onresize = k }
|