|
| 1 | + |
| 2 | +(function(b, u, v) { |
| 3 | + function x(b, f, e) { |
| 4 | + b = (b + "").match(/^(-?[0-9]+)(%)?$/); |
| 5 | + if (!b) return !1; |
| 6 | + var c = parseInt(b[1], 10); |
| 7 | + b[2] && (c *= f / 100); |
| 8 | + return 0 > c ? f + c + (e || 0) : c |
| 9 | + } |
| 10 | + |
| 11 | + function y(k, f) { |
| 12 | + function e() { |
| 13 | + function b() { |
| 14 | + g = +new Date; |
| 15 | + f.apply(e, t); |
| 16 | + c && (c = clearTimeout(c)) |
| 17 | + } |
| 18 | + var e = this, |
| 19 | + q = +new Date - g, |
| 20 | + t = arguments; |
| 21 | + c && (c = clearTimeout(c)); |
| 22 | + q > k ? b() : c = setTimeout(b, k - q) |
| 23 | + } |
| 24 | + var c, g = 0; |
| 25 | + b.guid && (e.guid = f.guid = f.guid || b.guid++); |
| 26 | + return e |
| 27 | + } |
| 28 | + b.Espy = function(k, f, e) { |
| 29 | + function c(a, d) { |
| 30 | + b.isPlainObject(a) && (d = a, a = null); |
| 31 | + b.extend(t.prototype, d); |
| 32 | + null !== a && (w = a) |
| 33 | + } |
| 34 | + |
| 35 | + function g(a) { |
| 36 | + if (a = |
| 37 | + q(a)) { |
| 38 | + var d = a.$el.offset()[a.settings.horizontal ? "left" : "top"] - p.offset[a.settings.horizontal ? "left" : "top"], |
| 39 | + h = a.$el[a.settings.horizontal ? "outerWidth" : "outerHeight"](); |
| 40 | + b.extend(a, { |
| 41 | + start: d, |
| 42 | + elSize: h, |
| 43 | + end: d + h |
| 44 | + }) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + function r(a) { |
| 49 | + // console.log("r(a) with a = ", a); |
| 50 | + if (a === v) b.each(m, r); |
| 51 | + else if (a = q(a)) { |
| 52 | + var d = p[a.settings.horizontal ? "width" : "height"], |
| 53 | + h = x(a.settings.size, d), |
| 54 | + d = p[a.settings.horizontal ? "left" : "top"] + x(a.settings.offset, d, -h), |
| 55 | + c = d + h, |
| 56 | + h = a.settings.contain ? d <= a.start && c >= a.end ? "inside" : d + h / 2 > a.start + a.elSize / 2 ? a.settings.horizontal ? "left" : |
| 57 | + "up" : a.settings.horizontal ? "right" : "down" : d > a.start && d < a.end || c > a.start && c < a.end || d <= a.start && c >= a.start || d <= a.end && c >= a.end ? "inside" : d > a.end ? a.settings.horizontal ? "left" : "up" : a.settings.horizontal ? "right" : "down"; |
| 58 | + a.state !== h && (a.state = h, "function" === typeof w && w.call(a.el, "inside" === h, h), "function" === typeof a.callback && a.callback.call(a.el, "inside" === h, h)) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + function s(a) { |
| 63 | + if (m.hasOwnProperty(a)) return a; |
| 64 | + if (b.isPlainObject(a) && m.hasOwnProperty(a.id)) return a.id; |
| 65 | + a = b(a)[0]; |
| 66 | + var d = !1; |
| 67 | + b.each(m, function(b, |
| 68 | + c) { |
| 69 | + c.el === a && (d = b) |
| 70 | + }); |
| 71 | + return d |
| 72 | + } |
| 73 | + |
| 74 | + // console.log("nothing yet"); |
| 75 | + function q(a) { |
| 76 | + return (a = s(a)) ? m[a] : !1 |
| 77 | + } |
| 78 | + "function" !== typeof f && (e = f, f = 0); |
| 79 | + // console.log("k was", k); |
| 80 | + var t = function(a) { |
| 81 | + b.extend(this, a) |
| 82 | + }, |
| 83 | + u = function(a, d, c, e) { |
| 84 | + this.id = a; |
| 85 | + this.el = d; |
| 86 | + this.$el = b(d); |
| 87 | + this.callback = c; |
| 88 | + this.settings = new t(e); |
| 89 | + this.configure = function(a, d) { |
| 90 | + b.isPlainObject(a) && (d = a, a = null); |
| 91 | + b.extend(this.settings, d); |
| 92 | + null !== a && (this.callback = a) |
| 93 | + } |
| 94 | + }, |
| 95 | + n = this, |
| 96 | + l = b(k); |
| 97 | + // console.log("l", l, "l==window", l[0]==window); |
| 98 | + k = b.fn.espy.defaults; |
| 99 | + // console.log("k is", k); |
| 100 | + var w, m = {}, |
| 101 | + z = 0; |
| 102 | + // because window.offset() is not defined in jQuery 3 (why?!?!?!), |
| 103 | + // we have to treat that as a special case (DF 1/28/19) |
| 104 | + if (l[0]==window) { offSET = { top: 0, left: 0 } } |
| 105 | + else { offSET = l.offset() } |
| 106 | + var p = { |
| 107 | + top: l.scrollTop(), |
| 108 | + left: l.scrollLeft(), |
| 109 | + width: l.innerWidth(), |
| 110 | + height: l.innerHeight(), |
| 111 | + offset: offSET |
| 112 | +/* |
| 113 | + offset: l.offset() || { |
| 114 | + top: 0, |
| 115 | + left: 0 |
| 116 | + } |
| 117 | +*/ |
| 118 | + }; |
| 119 | + // console.log("p", p); |
| 120 | + c(f, b.extend({}, k, e)); |
| 121 | + n.add = function(a, d, c) { |
| 122 | + b.isPlainObject(d) && (c = d, d = 0); |
| 123 | + b(a).each(function(a, b) { |
| 124 | + var e = s(b) || "s" + z++; |
| 125 | + m[e] = new u(e, b, d, c); |
| 126 | + g(e); |
| 127 | + r(e) |
| 128 | + }) |
| 129 | + }; |
| 130 | + n.configure = function(a, d, e) { |
| 131 | + "function" === typeof a ? (d = a, a = null, b.isPlainObject(d) && (e = d, d = null)) : b.isPlainObject(a) ? (e = a, d = a = null) : b.isPlainObject(d) && (e = d, d = null); |
| 132 | + null === a ? (c(d, e), b.each(m, function(a, b) { |
| 133 | + g(b) |
| 134 | + })) : b(a).each(function(a, b) { |
| 135 | + var c = q(b); |
| 136 | + c && (c.configure(d, e), g(b)) |
| 137 | + }) |
| 138 | + }; |
| 139 | + n.reload = function(a) { |
| 140 | + a === v ? b.each(m, function() { |
| 141 | + g(this.id) |
| 142 | + }) : |
| 143 | + b(a).each(function(a, b) { |
| 144 | + var c = s(b); |
| 145 | + c && (g(c), r(c)) |
| 146 | + }) |
| 147 | + }; |
| 148 | + n.remove = function(a) { |
| 149 | + b(a).each(function(a, b) { |
| 150 | + var c = s(b); |
| 151 | + c && delete m[c] |
| 152 | + }) |
| 153 | + }; |
| 154 | + n.destroy = function() { |
| 155 | + l.off(".espy"); |
| 156 | + m = {}; |
| 157 | + n = v |
| 158 | + }; |
| 159 | + n.resize = function() { |
| 160 | + b.each(m, function() { |
| 161 | + this.reloadOnResize && g(this) |
| 162 | + }); |
| 163 | + p.width = l.innerWidth(); |
| 164 | + p.height = l.innerHeight(); |
| 165 | + r() |
| 166 | + }; |
| 167 | + l.on("scroll.espy", y(k.delay, function() { |
| 168 | + p.top = l.scrollTop(); |
| 169 | + p.left = l.scrollLeft(); |
| 170 | + r() |
| 171 | + })); |
| 172 | + l.on("resize.espy", y(k.delay, function() { |
| 173 | + n.resize() |
| 174 | + })) |
| 175 | + }; |
| 176 | + b.fn.espy = function(k, f) { |
| 177 | + var e, c; |
| 178 | + e = f && f.context || u; |
| 179 | + var g = b.data(e, |
| 180 | + "espy") || b.data(e, "espy", new b.Espy(e)); |
| 181 | + "string" !== typeof k ? g.add(this, k, f) : (e = k, c = Array.prototype.slice.call(arguments), c[0] = this, "function" === typeof g[e] && g[e].apply(g, c)); |
| 182 | + return this |
| 183 | + }; |
| 184 | + b.fn.espy.defaults = { |
| 185 | + delay: 100, |
| 186 | + context: window, |
| 187 | + horizontal: 0, |
| 188 | + offset: 0, |
| 189 | + size: "100%", |
| 190 | + contain: 0, |
| 191 | + reloadOnResize: !0 |
| 192 | + } |
| 193 | +})(jQuery, window); |
| 194 | + |
0 commit comments