forked from o2web/scrollEvents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollEvents.min.js
1 lines (1 loc) · 7.25 KB
/
scrollEvents.min.js
1
!function(e,t){"function"==typeof define&&define.amd?define(["jquery","raf"],t):t(jQuery)}(this,function(e){function t(e,t,s,l){if(e.isVisible&&e.b<=scrollEvents.t||l&&e.b<=scrollEvents.t)if(t&&(e.isVisible=!1),!l&&s)e.up(e);else if(l&&s)return e.up;return!1}function s(e,t,s,l){if(e.isVisible&&e.t>=scrollEvents.b||l&&e.t>=scrollEvents.b)if(t&&(e.isVisible=!1),!l&&s)e.down(e);else if(l&&s)return e.down;return!1}function l(e,t,s,l){if(!e.isVisible&&e.t<scrollEvents.b&&e.b>scrollEvents.t||l&&e.t<scrollEvents.b&&e.b>scrollEvents.t)if(t&&(e.isVisible=!0),!l&&s)e.visible(e);else if(l&&s)return e.visible;return!1}function o(e,t,s,l){if(e.isTopVisible&&e.t<=scrollEvents.t||l&&e.t<=scrollEvents.t)if(t&&(e.isTopVisible=!1),!l&&s)e.topUp(e);else if(l&&s)return e.topUp;return!1}function r(e,t,s,l){if(!e.isTopVisible&&e.t>scrollEvents.t||l&&e.t>scrollEvents.t)if(t&&(e.isTopVisible=!0),!l&&s)e.topDown(e);else if(l&&s)return e.topDown;return!1}function n(e,t,s,l){if(e.isBottomVisible&&e.b<scrollEvents.b||l&&e.b<scrollEvents.b)if(t&&(e.isBottomVisible=!1),!l&&s)e.bottomUp(e);else if(l&&s)return e.bottomUp;return!1}function i(e,t,s,l){if(!e.isBottomVisible&&e.b>=scrollEvents.b||l&&e.b>=scrollEvents.b)if(t&&(e.isBottomVisible=!0),!l&&s)e.bottomDown(e);else if(l&&s)return e.bottomDown;return!1}function c(e,t,s,l){(e.isVisible&&e.b<=scrollEvents.t||l&&e.b<=scrollEvents.t)&&(t&&!e.up&&(e.isVisible=!1),(s||l)&&(e.rafref=h.raf.off(e.rafref,e.container,"scroll",e.travel).ref)),(e.isVisible&&e.t>=scrollEvents.b||l&&e.t>=scrollEvents.b)&&(t&&!e.down&&(e.isVisible=!1),(s||l)&&(e.rafref=h.raf.off(e.rafref,e.container,"scroll",e.travel).ref)),(!e.isVisible&&e.t<scrollEvents.b&&e.b>scrollEvents.t||l&&e.t<scrollEvents.b&&e.b>scrollEvents.t)&&(t&&!e.visible&&(e.isVisible=!0),(s||l)&&(e.rafref&&h.raf.off(e.rafref,e.container,"scroll",e.travel),e.rafref=h.raf.on(e.container,"scroll",{delta:function(){return minMax(Math.round((scrollEvents.t-(e.t-scrollEvents.wh))/(e.h+e.offset+e.offsetBottom+scrollEvents.wh)*e.round)/e.round,0,1)},selection:e.selection,index:e.i,height:e.h},e.travel).ref))}function a(e){e.checks=[],e.travel&&e.checks.push({event:e,fn:c,activate:!0,callback:!!e.travel}),(e.up||e.checkdown||e.visible)&&e.checks.push({event:e,fn:t,activate:!0,callback:!!e.up},{event:e,fn:s,activate:!0,callback:!!e.down},{event:e,fn:l,activate:!0,callback:!!e.visible}),(e.topUp||e.topDown)&&e.checks.push({event:e,fn:r,activate:!0,callback:!!e.topDown},{event:e,fn:o,activate:!0,callback:!!e.topUp}),(e.bottomUp||e.bottomDown)&&e.checks.push({event:e,fn:i,activate:!0,callback:!!e.bottomDown},{event:e,fn:n,activate:!0,callback:!!e.bottomUp})}function f(e){if("boolean"==typeof e)var t=!0,s=[];scrollEvents.t=$win.scrollTop(),scrollEvents.b=scrollEvents.t+scrollEvents.wh;for(var l=0;l<scrollEvents.selection.length;l++)for(var o=scrollEvents.selection[l],r=0;r<o.scrollEvents.length;r++)(function(e){if(!e.disabled)for(var l=0;l<e.checks.length;l++){var o=e.checks[l],r=o.fn(e,o.activate,o.callback,t);r&&s.push({callback:r,e:e})}})(o.scrollEvents[r]);if(t&&s.length){s.sort(sortCallbacksByDistance);for(var n=0;n<s.length;n++)s[n].callback&&s[n].callback(s[n].e)}}function v(){scrollEvents.wh=$win.height();for(var t=0;t<scrollEvents.selection.length;t++){var s=e(scrollEvents.selection[t]),l=s[0],o=s.outerHeight();l.currentStates.position=l.style.position,l.currentStates.top=l.style.top,l.style.position=l.initialStates.position,l.style.top=l.initialStates.top;for(var r=Math.round(s.offset().top),n=0;n<l.scrollEvents.length;n++){var i=l.scrollEvents[n];i.h=o,i.t=r-i.offset,i.b=i.t+i.h+i.offsetBottom}l.style.position=l.currentStates.position,l.style.top=l.currentStates.top}}function b(){h.raf.on("nextframe",function(){v(),f(!0)})}function u(t,s,l,o){if("resize"==s)b();else if("trigger"==s)f("update");else if("update"==s)b("update");else if("destroy"==s||"disable"==s||"enable"==s||"remove"==s||"get"==s||"set"==s||"eval"==s){for(var t=e(t),r=[],n=[],i=0;i<t.length;i++){var a=t[i];if(a.scrollEvents)if("destroy"==s||"remove"==s)r.push(a);else for(var v=0;v<a.scrollEvents.length;v++){var u=a.scrollEvents[v];if("eval"==s){var E=o?o:l,p=o?l:!1;if(u[E]&&(!p||p&&p==u.flag))return"travel"==E?{data:{delta:function(){return Math.round((scrollEvents.t-(u.t-scrollEvents.wh))/(u.h+scrollEvents.wh)*100)/100},selection:u.selection,index:u.i,height:u.h}}:u[E](u)}else l?u.flag==l&&("disable"==s?(u.disabled=!0,u.travel&&h.raf.off(u.container,"scroll",u.travel),u.disable&&"function"==typeof u.disable&&u.disable(u)):"enable"==s?(u.disabled=!1,u.travel&&(w.isVisible=!1,c(u,!0,!0)),u.enable&&"function"==typeof u.enable&&u.enable(u)):"set"==s?e.extend(!0,u,o):"get"==s&&n.push(u)):"disable"==s?(u.disabled=!0,u.travel&&h.raf.off(u.container,"scroll",u.travel)):"enable"==s?(u.disabled=!1,u.travel&&(u.isVisible=!1,c(u,!0,!0))):"set"==s?e.extend(!0,u,o):"get"==s&&n.push(u)}}if("destroy"==s||"remove"==s){r.sort(function(e,t){return t.scrollEvents.se-e.scrollEvents.se});for(var d=0;d<r.length;d++){var a=r[d];if(a.scrollEvents)for(var w=0;w<a.scrollEvents.length;w++)a.scrollEvents[w].travel&&h.raf.off(a.scrollEvents[w].rafref,a.container,"scroll",a.scrollEvents[w].travel);scrollEvents.selection.splice(a.scrollEvents.se,1),a.scrollEvents=[]}for(var i=0;i<scrollEvents.selection.length;i++)scrollEvents.selection[i].scrollEvents.se=i}if("destroy"!=s||scrollEvents.selection.length||(h.raf.off(scrollEvents.scrollRafref,"scroll",f),h.raf.off(scrollEvents.resizeRafref,"afterdocumentresize",b)),"get"==s)return n}return t}var h=this;$win=e(window),h.scrollEvents={selection:[],t:$win.scrollTop(),b:$win.height(),wh:$win.height()},Function.prototype.clone=function(){var e=this,t=function(){return e.apply(this,arguments)};for(var s in this)this.hasOwnProperty(s)&&(t[s]=this[s]);return t},minMax=function(e,t,s){return t>e?t:e>s?s:e},sortByOrder=function(e,t){return e.order<t.order?-1:e.order>t.order?1:0},sortCallbacksByDistance=function(e,t){var s=Math.abs(e.e.topUp||e.e.topDown?scrollEvents.t-e.e.t:scrollEvents.b-e.e.b),l=Math.abs(t.e.topUp||t.e.topDown?scrollEvents.t-t.e.t:scrollEvents.b-t.e.b);return l>s?1:s>l?-1:0},moveCheck=function(){},e.extend(e.fn,{scrollEvents:function(t,s,l){return"string"==typeof t?u(this,t,s,l):(e(this).each(function(s){var l=e.extend(!0,{selection:e(this),container:$win,flag:!1,order:0,offset:0,offsetBottom:0,round:100,visible:!1,up:!1,down:!1,topUp:!1,topDown:!1,bottomUp:!1,bottomDown:!1,travel:!1,disable:!1,enable:!1,isVisible:!1,isTopVisible:!1,isBottomVisible:!1,h:e(this).outerHeight(),t:0,b:e(this).outerHeight(),i:s,disabled:!1,checks:[]},t);l.travel=t.travel?t.travel.clone():!1,a(l),l.checks.sort(sortByOrder);for(var o=!1,r=0;r<scrollEvents.selection.length;r++)scrollEvents.selection[r]==this&&(o=!0);o||(scrollEvents.selection.push(this),l.se=scrollEvents.selection.length,this.initialStates={position:e(this).css("position"),top:e(this).css("top")},this.currentStates={}),this.scrollEvents||(this.scrollEvents=[]),this.scrollEvents.push(l),this.scrollEvents.sort(sortByOrder)}),void 0!=scrollEvents.scrollRafref&&h.raf.off(scrollEvents.scrollRafref,"scroll",f),scrollEvents.scrollRafref=h.raf.on("scroll",f).ref,void 0!=scrollEvents.resizeRafref&&h.raf.off(scrollEvents.resizeRafref,"afterdocumentresize",b),scrollEvents.resizeRafref=h.raf.on("afterdocumentresize",b).ref,this)}}),e(document).ready(function(){b()}),$win.on("load",function(){b("update")})});