waypoint.min.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*!
  2. Waypoints - 4.0.0
  3. Copyright © 2011-2015 Caleb Troughton
  4. Licensed under the MIT license.
  5. https://github.com/imakewebthings/waypoints/blob/master/licenses.txt
  6. */
  7. ! function() {
  8. "use strict";
  9. function t(o) {
  10. if (!o) throw new Error("No options passed to Waypoint constructor");
  11. if (!o.element) throw new Error("No element option passed to Waypoint constructor");
  12. if (!o.handler) throw new Error("No handler option passed to Waypoint constructor");
  13. this.key = "waypoint-" + e, this.options = t.Adapter.extend({}, t.defaults, o), this.element = this.options.element, this.adapter = new t.Adapter(this.element), this.callback = o.handler, this.axis = this.options.horizontal ? "horizontal" : "vertical", this.enabled = this.options.enabled, this.triggerPoint = null, this.group = t.Group.findOrCreate({
  14. name: this.options.group,
  15. axis: this.axis
  16. }), this.context = t.Context.findOrCreateByElement(this.options.context), t.offsetAliases[this.options.offset] && (this.options.offset = t.offsetAliases[this.options.offset]), this.group.add(this), this.context.add(this), i[this.key] = this, e += 1
  17. }
  18. var e = 0,
  19. i = {};
  20. t.prototype.queueTrigger = function(t) {
  21. this.group.queueTrigger(this, t)
  22. }, t.prototype.trigger = function(t) {
  23. this.enabled && this.callback && this.callback.apply(this, t)
  24. }, t.prototype.destroy = function() {
  25. this.context.remove(this), this.group.remove(this), delete i[this.key]
  26. }, t.prototype.disable = function() {
  27. return this.enabled = !1, this
  28. }, t.prototype.enable = function() {
  29. return this.context.refresh(), this.enabled = !0, this
  30. }, t.prototype.next = function() {
  31. return this.group.next(this)
  32. }, t.prototype.previous = function() {
  33. return this.group.previous(this)
  34. }, t.invokeAll = function(t) {
  35. var e = [];
  36. for (var o in i) e.push(i[o]);
  37. for (var n = 0, r = e.length; r > n; n++) e[n][t]()
  38. }, t.destroyAll = function() {
  39. t.invokeAll("destroy")
  40. }, t.disableAll = function() {
  41. t.invokeAll("disable")
  42. }, t.enableAll = function() {
  43. t.invokeAll("enable")
  44. }, t.refreshAll = function() {
  45. t.Context.refreshAll()
  46. }, t.viewportHeight = function() {
  47. return window.innerHeight || document.documentElement.clientHeight
  48. }, t.viewportWidth = function() {
  49. return document.documentElement.clientWidth
  50. }, t.adapters = [], t.defaults = {
  51. context: window,
  52. continuous: !0,
  53. enabled: !0,
  54. group: "default",
  55. horizontal: !1,
  56. offset: 0
  57. }, t.offsetAliases = {
  58. "bottom-in-view": function() {
  59. return this.context.innerHeight() - this.adapter.outerHeight()
  60. },
  61. "right-in-view": function() {
  62. return this.context.innerWidth() - this.adapter.outerWidth()
  63. }
  64. }, window.Waypoint = t
  65. }(),
  66. function() {
  67. "use strict";
  68. function t(t) {
  69. window.setTimeout(t, 1e3 / 60)
  70. }
  71. function e(t) {
  72. this.element = t, this.Adapter = n.Adapter, this.adapter = new this.Adapter(t), this.key = "waypoint-context-" + i, this.didScroll = !1, this.didResize = !1, this.oldScroll = {
  73. x: this.adapter.scrollLeft(),
  74. y: this.adapter.scrollTop()
  75. }, this.waypoints = {
  76. vertical: {},
  77. horizontal: {}
  78. }, t.waypointContextKey = this.key, o[t.waypointContextKey] = this, i += 1, this.createThrottledScrollHandler(), this.createThrottledResizeHandler()
  79. }
  80. var i = 0,
  81. o = {},
  82. n = window.Waypoint,
  83. r = window.onload;
  84. e.prototype.add = function(t) {
  85. var e = t.options.horizontal ? "horizontal" : "vertical";
  86. this.waypoints[e][t.key] = t, this.refresh()
  87. }, e.prototype.checkEmpty = function() {
  88. var t = this.Adapter.isEmptyObject(this.waypoints.horizontal),
  89. e = this.Adapter.isEmptyObject(this.waypoints.vertical);
  90. t && e && (this.adapter.off(".waypoints"), delete o[this.key])
  91. }, e.prototype.createThrottledResizeHandler = function() {
  92. function t() {
  93. e.handleResize(), e.didResize = !1
  94. }
  95. var e = this;
  96. this.adapter.on("resize.waypoints", function() {
  97. e.didResize || (e.didResize = !0, n.requestAnimationFrame(t))
  98. })
  99. }, e.prototype.createThrottledScrollHandler = function() {
  100. function t() {
  101. e.handleScroll(), e.didScroll = !1
  102. }
  103. var e = this;
  104. this.adapter.on("scroll.waypoints", function() {
  105. (!e.didScroll || n.isTouch) && (e.didScroll = !0, n.requestAnimationFrame(t))
  106. })
  107. }, e.prototype.handleResize = function() {
  108. n.Context.refreshAll()
  109. }, e.prototype.handleScroll = function() {
  110. var t = {},
  111. e = {
  112. horizontal: {
  113. newScroll: this.adapter.scrollLeft(),
  114. oldScroll: this.oldScroll.x,
  115. forward: "right",
  116. backward: "left"
  117. },
  118. vertical: {
  119. newScroll: this.adapter.scrollTop(),
  120. oldScroll: this.oldScroll.y,
  121. forward: "down",
  122. backward: "up"
  123. }
  124. };
  125. for (var i in e) {
  126. var o = e[i],
  127. n = o.newScroll > o.oldScroll,
  128. r = n ? o.forward : o.backward;
  129. for (var s in this.waypoints[i]) {
  130. var a = this.waypoints[i][s],
  131. l = o.oldScroll < a.triggerPoint,
  132. h = o.newScroll >= a.triggerPoint,
  133. p = l && h,
  134. u = !l && !h;
  135. (p || u) && (a.queueTrigger(r), t[a.group.id] = a.group)
  136. }
  137. }
  138. for (var c in t) t[c].flushTriggers();
  139. this.oldScroll = {
  140. x: e.horizontal.newScroll,
  141. y: e.vertical.newScroll
  142. }
  143. }, e.prototype.innerHeight = function() {
  144. return this.element == this.element.window ? n.viewportHeight() : this.adapter.innerHeight()
  145. }, e.prototype.remove = function(t) {
  146. delete this.waypoints[t.axis][t.key], this.checkEmpty()
  147. }, e.prototype.innerWidth = function() {
  148. return this.element == this.element.window ? n.viewportWidth() : this.adapter.innerWidth()
  149. }, e.prototype.destroy = function() {
  150. var t = [];
  151. for (var e in this.waypoints)
  152. for (var i in this.waypoints[e]) t.push(this.waypoints[e][i]);
  153. for (var o = 0, n = t.length; n > o; o++) t[o].destroy()
  154. }, e.prototype.refresh = function() {
  155. var t, e = this.element == this.element.window,
  156. i = e ? void 0 : this.adapter.offset(),
  157. o = {};
  158. this.handleScroll(), t = {
  159. horizontal: {
  160. contextOffset: e ? 0 : i.left,
  161. contextScroll: e ? 0 : this.oldScroll.x,
  162. contextDimension: this.innerWidth(),
  163. oldScroll: this.oldScroll.x,
  164. forward: "right",
  165. backward: "left",
  166. offsetProp: "left"
  167. },
  168. vertical: {
  169. contextOffset: e ? 0 : i.top,
  170. contextScroll: e ? 0 : this.oldScroll.y,
  171. contextDimension: this.innerHeight(),
  172. oldScroll: this.oldScroll.y,
  173. forward: "down",
  174. backward: "up",
  175. offsetProp: "top"
  176. }
  177. };
  178. for (var r in t) {
  179. var s = t[r];
  180. for (var a in this.waypoints[r]) {
  181. var l, h, p, u, c, d = this.waypoints[r][a],
  182. f = d.options.offset,
  183. w = d.triggerPoint,
  184. y = 0,
  185. g = null == w;
  186. d.element !== d.element.window && (y = d.adapter.offset()[s.offsetProp]), "function" == typeof f ? f = f.apply(d) : "string" == typeof f && (f = parseFloat(f), d.options.offset.indexOf("%") > -1 && (f = Math.ceil(s.contextDimension * f / 100))), l = s.contextScroll - s.contextOffset, d.triggerPoint = y + l - f, h = w < s.oldScroll, p = d.triggerPoint >= s.oldScroll, u = h && p, c = !h && !p, !g && u ? (d.queueTrigger(s.backward), o[d.group.id] = d.group) : !g && c ? (d.queueTrigger(s.forward), o[d.group.id] = d.group) : g && s.oldScroll >= d.triggerPoint && (d.queueTrigger(s.forward), o[d.group.id] = d.group)
  187. }
  188. }
  189. return n.requestAnimationFrame(function() {
  190. for (var t in o) o[t].flushTriggers()
  191. }), this
  192. }, e.findOrCreateByElement = function(t) {
  193. return e.findByElement(t) || new e(t)
  194. }, e.refreshAll = function() {
  195. for (var t in o) o[t].refresh()
  196. }, e.findByElement = function(t) {
  197. return o[t.waypointContextKey]
  198. }, window.onload = function() {
  199. r && r(), e.refreshAll()
  200. }, n.requestAnimationFrame = function(e) {
  201. var i = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || t;
  202. i.call(window, e)
  203. }, n.Context = e
  204. }(),
  205. function() {
  206. "use strict";
  207. function t(t, e) {
  208. return t.triggerPoint - e.triggerPoint
  209. }
  210. function e(t, e) {
  211. return e.triggerPoint - t.triggerPoint
  212. }
  213. function i(t) {
  214. this.name = t.name, this.axis = t.axis, this.id = this.name + "-" + this.axis, this.waypoints = [], this.clearTriggerQueues(), o[this.axis][this.name] = this
  215. }
  216. var o = {
  217. vertical: {},
  218. horizontal: {}
  219. },
  220. n = window.Waypoint;
  221. i.prototype.add = function(t) {
  222. this.waypoints.push(t)
  223. }, i.prototype.clearTriggerQueues = function() {
  224. this.triggerQueues = {
  225. up: [],
  226. down: [],
  227. left: [],
  228. right: []
  229. }
  230. }, i.prototype.flushTriggers = function() {
  231. for (var i in this.triggerQueues) {
  232. var o = this.triggerQueues[i],
  233. n = "up" === i || "left" === i;
  234. o.sort(n ? e : t);
  235. for (var r = 0, s = o.length; s > r; r += 1) {
  236. var a = o[r];
  237. (a.options.continuous || r === o.length - 1) && a.trigger([i])
  238. }
  239. }
  240. this.clearTriggerQueues()
  241. }, i.prototype.next = function(e) {
  242. this.waypoints.sort(t);
  243. var i = n.Adapter.inArray(e, this.waypoints),
  244. o = i === this.waypoints.length - 1;
  245. return o ? null : this.waypoints[i + 1]
  246. }, i.prototype.previous = function(e) {
  247. this.waypoints.sort(t);
  248. var i = n.Adapter.inArray(e, this.waypoints);
  249. return i ? this.waypoints[i - 1] : null
  250. }, i.prototype.queueTrigger = function(t, e) {
  251. this.triggerQueues[e].push(t)
  252. }, i.prototype.remove = function(t) {
  253. var e = n.Adapter.inArray(t, this.waypoints);
  254. e > -1 && this.waypoints.splice(e, 1)
  255. }, i.prototype.first = function() {
  256. return this.waypoints[0]
  257. }, i.prototype.last = function() {
  258. return this.waypoints[this.waypoints.length - 1]
  259. }, i.findOrCreate = function(t) {
  260. return o[t.axis][t.name] || new i(t)
  261. }, n.Group = i
  262. }(),
  263. function() {
  264. "use strict";
  265. function t(t) {
  266. this.$element = e(t)
  267. }
  268. var e = window.jQuery,
  269. i = window.Waypoint;
  270. e.each(["innerHeight", "innerWidth", "off", "offset", "on", "outerHeight", "outerWidth", "scrollLeft", "scrollTop"], function(e, i) {
  271. t.prototype[i] = function() {
  272. var t = Array.prototype.slice.call(arguments);
  273. return this.$element[i].apply(this.$element, t)
  274. }
  275. }), e.each(["extend", "inArray", "isEmptyObject"], function(i, o) {
  276. t[o] = e[o]
  277. }), i.adapters.push({
  278. name: "jquery",
  279. Adapter: t
  280. }), i.Adapter = t
  281. }(),
  282. function() {
  283. "use strict";
  284. function t(t) {
  285. return function() {
  286. var i = [],
  287. o = arguments[0];
  288. return t.isFunction(arguments[0]) && (o = t.extend({}, arguments[1]), o.handler = arguments[0]), this.each(function() {
  289. var n = t.extend({}, o, {
  290. element: this
  291. });
  292. "string" == typeof n.context && (n.context = t(this).closest(n.context)[0]), i.push(new e(n))
  293. }), i
  294. }
  295. }
  296. var e = window.Waypoint;
  297. window.jQuery && (window.jQuery.fn.waypoint = t(window.jQuery)), window.Zepto && (window.Zepto.fn.waypoint = t(window.Zepto))
  298. }();