1/** 2 * This is a forked version, for usage check please: https://github.com/WPDreams/isInViewport/blob/master/readme.md 3 * 4 * @author Mudit Ameta 5 * @see https://github.com/WPDreams/isInViewport/blob/master/readme.md 6 * @license https://github.com/zeusdeux/isInViewport/blob/master/license.md MIT 7 */ 8;(function (window) { 9 'use strict'; 10 var $ = typeof aspjQuery != 'undefined' ? aspjQuery : window.jQuery; 11 12 // expose isInViewport as a custom pseudo-selector 13 $.extend($.expr.pseudos || $.expr[':'], { 14 // if $.expr.createPseudo is available, use it 15 'in-viewport': $.expr.createPseudo 16 ? $.expr.createPseudo(function (argsString) { 17 return function (currElement) { 18 return isInViewport(currElement, getSelectorArgs(argsString)); 19 }; 20 }) 21 : function (currObj, index, meta) { 22 return isInViewport(currObj, getSelectorArgs(meta[3])); 23 } 24 }); 25 26 27 // expose isInViewport as a function too 28 // this lets folks pass around actual objects as options (like custom viewport) 29 // and doesn't tie 'em down to strings. It also prevents isInViewport from 30 // having to look up and wrap the dom element corresponding to the viewport selector 31 $.fn.isInViewport = function (options) { 32 return this.filter(function (i, el) { 33 return isInViewport(el, options); 34 }) 35 }; 36 37 $.fn.run = run; 38 39 // lets you chain any arbitrary function or an array of functions and returns a jquery object 40 function run(args) { 41 var this$1 = this; 42 43 if (arguments.length === 1 && typeof args === 'function') { 44 args = [args]; 45 } 46 47 if (!(args instanceof Array)) { 48 throw new SyntaxError('isInViewport: Argument(s) passed to .do/.run should be a function or an array of functions') 49 } 50 51 args.forEach(function (arg) { 52 if (typeof arg !== 'function') { 53 console.warn('isInViewport: Argument(s) passed to .do/.run should be a function or an array of functions'); 54 console.warn('isInViewport: Ignoring non-function values in array and moving on'); 55 } else { 56 [].slice.call(this$1).forEach(function (t) { 57 return arg.call($(t)); 58 }); 59 } 60 }); 61 62 return this 63 } 64 65 66 // gets the width of the scrollbar 67 function getScrollbarWidth(viewport) { 68 // append a div that has 100% width to get true width of viewport 69 var el = $('<div></div>').css({ 70 width: '100%' 71 }); 72 viewport.append(el); 73 74 // subtract true width from the viewport width which is inclusive 75 // of scrollbar by default 76 var scrollBarWidth = viewport.width() - el.width(); 77 78 // remove our element from DOM 79 el.remove(); 80 return scrollBarWidth 81 } 82 83 84 // Returns true if DOM element `element` is in viewport 85 function isInViewport(element, options) { 86 var ref = element.getBoundingClientRect(); 87 var top = ref.top; 88 var bottom = ref.bottom; 89 var left = ref.left; 90 var right = ref.right; 91 92 var settings = $.extend({ 93 tolerance: 0, 94 viewport: window 95 }, options); 96 var isVisibleFlag = false; 97 var $viewport = settings.viewport.jquery ? settings.viewport : $(settings.viewport); 98 99 if (!$viewport.length) { 100 console.warn('isInViewport: The viewport selector you have provided matches no element on page.'); 101 console.warn('isInViewport: Defaulting to viewport as window'); 102 $viewport = $(window); 103 } 104 105 var $viewportHeight = $viewport.height(); 106 var $viewportWidth = $viewport.width(); 107 var typeofViewport = $viewport[0].toString(); 108 109 // if the viewport is other than window recalculate the top, 110 // bottom,left and right wrt the new viewport 111 // the [object DOMWindow] check is for window object type in PhantomJS 112 if ($viewport[0] !== window && typeofViewport !== '[object Window]' && typeofViewport !== '[object DOMWindow]') { 113 // use getBoundingClientRect() instead of $.Offset() 114 // since the original top/bottom positions are calculated relative to browser viewport and n
114ot document 115 var viewportRect = $viewport[0].getBoundingClientRect(); 116 117 // recalculate these relative to viewport 118 top = top - viewportRect.top; 119 bottom = bottom - viewportRect.top; 120 left = left - viewportRect.left; 121 right = right - viewportRect.left; 122 123 // get the scrollbar width from cache or calculate it 124 isInViewport.scrollBarWidth = isInViewport.scrollBarWidth || getScrollbarWidth($viewport); 125 126 // remove the width of the scrollbar from the viewport width 127 $viewportWidth -= isInViewport.scrollBarWidth; 128 } 129 130 // handle falsy, non-number and non-integer tolerance value 131 // same as checking using isNaN and then setting to 0 132 // bitwise operators deserve some love too you know 133 settings.tolerance = ~~Math.round(parseFloat(settings.tolerance)); 134 135 // the element is NOT in viewport iff it is completely out of 136 // viewport laterally or if it is completely out of the tolerance 137 // region. Therefore, if it is partially in view then it is considered 138 // to be in the viewport and hence true is returned. Because we have adjusted 139 // the left/right positions relative to the viewport, we should check the 140 // element's right against the viewport's 0 (left side), and the element's 141 // left against the viewport's width to see if it is outside of the viewport. 142 143 if (right <= 0 || left >= $viewportWidth) { 144 return isVisibleFlag 145 } 146 147 // if the element is bound to some tolerance 148 isVisibleFlag = settings.tolerance ? top >= settings.tolerance && bottom < ( $viewportHeight - settings.tolerance ) : bottom > 0 && top <= $viewportHeight; 149 150 return isVisibleFlag 151 } 152 153 154 // get the selector args from the args string proved by Sizzle 155 function getSelectorArgs(argsString) { 156 if (argsString) { 157 var args = argsString.split(','); 158 159 // when user only gives viewport and no tolerance 160 if (args.length === 1 && isNaN(args[0])) { 161 args[1] = args[0]; 162 args[0] = void 0; 163 } 164 165 return { 166 tolerance: args[0] ? args[0].trim() : void 0, 167 viewport: args[1] ? $(args[1].trim()) : void 0 168 } 169 } 170 return {} 171 } 172})(window);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.