-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.scrollcallback.min.js
7 lines (7 loc) · 1.81 KB
/
jquery.scrollcallback.min.js
1
2
3
4
5
6
7
/**
* ScrollCallback
* Copyright (c) 2015 Pavel Khoroshkov. Licensed under the [MIT license](https://github.com/pgooood/scrollcallback/blob/master/LICENSE).
* @author Pavel Khoroshkov aka pgood
* @link https://github.com/pgooood/scrollcallback
*/
!function(t){function e(e,n,i,s){var r=[],c=t(s?s:window),f={left:0,top:0},l=function(){var t,e=c.scrollLeft(),o=c.scrollTop();switch(!0){case o>f.top:t="down";break;case o<f.top:t="up";break;case e>f.left:t="right";break;case e<f.left:t="left"}return f.left=e,f.top=o,t};t(e).each(function(){r.push(new o(this,n,i,s))}),c.scroll(function(){for(var t=l(),e=0,o=r.length;o>e;e++)r[e].handler(t)}).scroll()}function o(e,o,n,i){this.charged=!1,this.$e=t(e),this.$context=i&&!t.isWindow(i)?this.$e.parents(i):t(window),this.onIn=function(t){this.charged||"function"!=typeof o||o(this.$e.get(0),t),this.charged=!0},this.onOut=function(t){this.charged&&"function"==typeof n&&n(this.$e.get(0),t),this.charged=!1}}t.fn.scrollcallback=function(o){var n={context:window};switch(typeof o){case"function":n=t.extend(n,{inHandler:o});break;case"object":if(o.inHandler||o.outHandler){n=t.extend(n,o);break}default:return!1}return new e(this,n.inHandler,n.outHandler,n.context)},o.prototype.offset=function(t){var e,o={left:0,top:0};do e=t.offset(),o.left+=e.left,o.top+=e.top,t=t.offsetParent();while("body"==!t.prop("tagName").toLowerCase());return o},o.prototype.visible=function(){var e=this.$context,o=this.offset(this.$e);if(!t.isWindow(this.$context.get(0))){var n=this.offset(this.$context);o.left-=n.left-this.$context.scrollLeft(),o.top-=n.top-this.$context.scrollTop()}return e.scrollLeft()+e.width()>o.left&&e.scrollLeft()<o.left+this.$e.width()&&e.scrollTop()+e.height()>o.top&&e.scrollTop()<o.top+this.$e.height()},o.prototype.handler=function(t){this.visible()?this.onIn(t):this.onOut(t)}}(jQuery);