1/* 2* jQuery.appear 3* https://github.com/bas2k/jquery.appear/ 4* http://code.google.com/p/jquery-appear/ 5* http://bas2k.ru/ 6* 7* Copyright (c) 2009 Michael Hixson 8* Copyright (c) 2012-2014 Alexander Brovikov 9* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php) 10*/ 11(function($) { 12 13 "use strict" 14 15 $.fn.appear = function(fn, options) { 16 17 var settings = $.extend({ 18 19 //arbitrary data to pass to fn 20 data: undefined, 21 22 //call fn only on the first appear? 23 one: true, 24 25 // X & Y accuracy 26 accX: 0, 27 accY: 0 28 29 }, options); 30 31 return this.each(function() { 32 33 var t = $(this); 34 35 //whether the element is currently visible 36 t.appeared = false; 37 38 if (!fn) { 39 40 //trigger the custom event 41 t.trigger('appear', settings.data); 42 return; 43 } 44 45 var w = $(window); 46 47 //fires the appear event when appropriate 48 var check = function() { 49 50 //is the element hidden? 51 if (!t.is(':visible')) { 52 53 //it became hidden 54 t.appeared = false; 55 return; 56 } 57 58 //is the element inside the visible window? 59 var a = w.scrollLeft(); 60 var b = w.scrollTop(); 61 var o = t.offset(); 62 var x = o.left; 63 var y = o.top; 64 65 var ax = settings.accX; 66 var ay = settings.accY; 67 var th = t.height(); 68 var wh = w.height(); 69 var tw = t.width(); 70 var ww = w.width(); 71 72 if (y + th + ay >= b && 73 y <= b + wh + ay && 74 x + tw + ax >= a && 75 x <= a + ww + ax) { 76 77 //trigger the custom event 78 if (!t.appeared) t.trigger('appear', settings.data); 79 80 } else { 81 82 //it scrolled out of view 83 t.appeared = false; 84 } 85 }; 86 87 //create a modified fn with some additional logic 88 var modifiedFn = function() { 89 90 //mark the element as visible 91 t.appeared = true; 92 93 //is this supposed to happen only once? 94 if (settings.one) { 95 96 //remove the check 97 w.unbind('scroll', check); 98 var i = $.inArray(check, $.fn.appear.checks); 99 if (i >= 0) $.fn.appear.checks.splice(i, 1); 100 } 101 102 //trigger the original fn 103 fn.apply(this, arguments); 104 }; 105 106 //bind the modified fn to the element 107 if (settings.one) t.one('appear', settings.data, modifiedFn); 108 else t.bind('appear', settings.data, modifiedFn); 109 110 //check whenever the window scrolls 111 w.scroll(check); 112 113 //check whenever the dom changes 114 $.fn.appear.checks.push(check); 115 116 //check now 117 (check)(); 118 }); 119 }; 120 121 //keep a queue of appearance checks 122 $.extend($.fn.appear, { 123 124 checks: [], 125 timeout: null, 126 127 //process the queue 128 checkAll: function() { 129 var length = $.fn.appear.checks.length; 130 if (length > 0) while (length--) ($.fn.appear.checks[length])(); 131 }, 132 133 //check the queue asynchronously 134 run: function() { 135 if ($.fn.appear.timeout) clearTimeout($.fn.appear.timeout); 136 $.fn.appear.timeout = setTimeout($.fn.appear.checkAll, 20); 137 } 138 }); 139 140 //run checks when these methods are called 141 $.each(['append', 'prepend', 'after', 'before', 'attr', 142 'removeAttr', 'addClass', 'removeClass', 'toggleClass', 143 'remove', 'css', 'show', 'hide'], function(i, n) { 144 var old = $.fn[n]; 145 if (old) { 146 $.fn[n] = function() { 147 var r = old.apply(this, arguments); 148 $.fn.appear.run(); 149 return r; 150 } 151 } 152 }); 153 154 })(jQuery);
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.