1 line
5.2 KiB
JavaScript
1 line
5.2 KiB
JavaScript
|
!function(t){"undefined"!=typeof define&&define.amd?define([],t):"undefined"!=typeof module&&module.exports?module.exports=t():t()}(function(){function t(){if(r.viewportTop=l(),r.viewportBottom=r.viewportTop+r.viewportHeight,r.documentHeight=y(),r.documentHeight!==I){for(H=a.length;H--;)a[H].recalculateLocation();I=r.documentHeight}}function i(){r.viewportHeight=V(),t(),e()}function o(){clearTimeout(E),E=setTimeout(i,100)}function e(){for(T=a.length;T--;)a[T].update();for(T=a.length;T--;)a[T].triggerCallbacks()}function s(t,i){function o(t){if(0!==t.length)for(V=t.length;V--;)y=t[V],y.callback.call(e,k),y.isOne&&t.splice(V,1)}var e=this;this.watchItem=t,i?i===+i?this.offsets={top:i,bottom:i}:this.offsets={top:i.top||g.top,bottom:i.bottom||g.bottom}:this.offsets=g,this.callbacks={};for(var s=0,n=v.length;s<n;s++)e.callbacks[v[s]]=[];this.locked=!1;var h,c,l,a,V,y;this.triggerCallbacks=function(){switch(this.isInViewport&&!h&&o(this.callbacks[w]),this.isFullyInViewport&&!c&&o(this.callbacks[u]),this.isAboveViewport!==l&&this.isBelowViewport!==a&&(o(this.callbacks[p]),c||this.isFullyInViewport||(o(this.callbacks[u]),o(this.callbacks[m])),h||this.isInViewport||(o(this.callbacks[w]),o(this.callbacks[f]))),!this.isFullyInViewport&&c&&o(this.callbacks[m]),!this.isInViewport&&h&&o(this.callbacks[f]),this.isInViewport!==h&&o(this.callbacks[p]),!0){case h!==this.isInViewport:case c!==this.isFullyInViewport:case l!==this.isAboveViewport:case a!==this.isBelowViewport:o(this.callbacks[b])}h=this.isInViewport,c=this.isFullyInViewport,l=this.isAboveViewport,a=this.isBelowViewport},this.recalculateLocation=function(){if(!this.locked){var t=this.top,i=this.bottom;if(this.watchItem.nodeName){var e=this.watchItem.style.display;"none"===e&&(this.watchItem.style.display="");var s=this.watchItem.getBoundingClientRect();this.top=s.top+r.viewportTop,this.bottom=s.bottom+r.viewportTop,"none"===e&&(this.watchItem.style.display=e)}else this.watchItem===+this.watchItem?this.watchItem>0?this.top=this.bottom=this.watchItem:this.top=this.bottom=r.documentHeight-this.watchItem:(this.top=this.watchItem.top,this.bottom=this.watchItem.bottom);this.top-=this.offsets.top,this.bottom+=this.offsets.bottom,this.height=this.bottom-this.top,void 0===t&&void 0===i||this.top===t&&this.bottom===i||o(this.callbacks[d])}},this.recalculateLocation(),this.update(),h=this.isInViewport,c=this.isFullyInViewport,l=this.isAboveViewport,a=this.isBelowViewport}function n(i){k=i,t(),e()}var h="undefined"==typeof window,c=!h,l=function(){return h?0:window.pageYOffset||document.documentElement&&document.documentElement.scrollTop||document.body.scrollTop},r={},a=[],p="visibilityChange",w="enterViewport",u="fullyEnterViewport",f="exitViewport",m="partiallyExitViewport",d="locationChange",b="stateChange",v=[p,w,u,f,m,d,b],g={top:0,bottom:0},V=function(){return h?0:window.innerHeight||document.documentElement.clientHeight},y=function(){return h?0:Math.max(document.body.scrollHeight,document.documentElement.scrollHeight,document.body.offsetHeight,document.documentElement.offsetHeight,document.documentElement.clientHeight)};r.viewportTop=null,r.viewportBottom=null,r.documentHeight=null,r.viewportHeight=V();var I,k,H,E,T;s.prototype={on:function(t,i,o){switch(!0){case t===p&&!this.isInViewport&&this.isAboveViewport:case t===w&&this.isInViewport:case t===u&&this.isFullyInViewport:case t===f&&this.isAboveViewport&&!this.isInViewport:case t===m&&this.isAboveViewport:if(i.call(this,k),o)return}if(!this.callbacks[t])throw new Error("Tried to add a scroll monitor listener of type "+t+". Your options are: "+v.join(", "));this.callbacks[t].push({callback:i,isOne:o||!1})},off:function(t,i){if(!this.callbacks[t])throw new Error("Tried to remove a scroll monitor listener of type "+t+". Your options are: "+v.join(", "));for(var o,e=0;o=this.callbacks[t][e];e++)if(o.callback===i){this.callbacks[t].splice(e,1);break}},one:function(t,i){this.on(t,i,!0)},recalculateSize:function(){this.height=this.watchItem.offsetHeight+this.offsets.top+this.offsets.bottom,this.bottom=this.top+this.height},update:fun
|