1 line
2.1 KiB
JavaScript
1 line
2.1 KiB
JavaScript
|
!function(n,e){function t(e){var t,r=n("<div></div>").css({width:"100%"});return e.append(r),t=e.width()-r.width(),r.remove(),t}function r(o,i){var a=o.getBoundingClientRect(),s=a.top,c=a.bottom,u=a.left,l=a.right,d=n.extend({tolerance:0,viewport:e},i),f=!1,p=d.viewport.jquery?d.viewport:n(d.viewport);p.length||(console.warn("isInViewport: The viewport selector you have provided matches no element on page."),console.warn("isInViewport: Defaulting to viewport as window"),p=n(e));var w=p.height(),h=p.width(),v=p[0].toString();if(p[0]!==e&&"[object Window]"!==v&&"[object DOMWindow]"!==v){var g=p[0].getBoundingClientRect();s-=g.top,c-=g.top,u-=g.left,l-=g.left,r.scrollBarWidth=r.scrollBarWidth||t(p),h-=r.scrollBarWidth}return d.tolerance=~~Math.round(parseFloat(d.tolerance)),d.tolerance<0&&(d.tolerance=w+d.tolerance),l<=0||u>=h?f:f=d.tolerance?s<=d.tolerance&&c>=d.tolerance:c>0&&s<=w}String.prototype.hasOwnProperty("trim")||(String.prototype.trim=function(){return this.replace(/^\s*(.*?)\s*$/,"$1")});var o=function(e){if(1===arguments.length&&"function"==typeof e&&(e=[e]),!(e instanceof Array))throw new SyntaxError("isInViewport: Argument(s) passed to .do/.run should be a function or an array of functions");for(var t=0;t<e.length;t++)if("function"==typeof e[t])for(var r=0;r<this.length;r++)e[t].call(n(this[r]));else console.warn("isInViewport: Argument(s) passed to .do/.run should be a function or an array of functions"),console.warn("isInViewport: Ignoring non-function values in array and moving on");return this};n.fn.do=function(n){return console.warn("isInViewport: .do is deprecated as it causes issues in IE and some browsers since it's a reserved word. Use $.fn.run instead i.e., $(el).run(fn)."),o(n)},n.fn.run=o;var i=function(e){if(e){var t=e.split(",");return 1===t.length&&isNaN(t[0])&&(t[1]=t[0],t[0]=void 0),{tolerance:t[0]?t[0].trim():void 0,viewport:t[1]?n(t[1].trim()):void 0}}return{}};n.extend(n.expr[":"],{"in-viewport":n.expr.createPseudo?n.expr.createPseudo(function(n){return function(e){return r(e,i(n))}}):function(n,e,t){return r(n,i(t[3]))}}),n.fn.isInViewport=function(n){return this.filter(function(e,t){return r(t,n)})}}(jQuery,window);
|