1/* 2 * jQuery appear plugin 3 * 4 * Copyright (c) 2012 Andrey Sidorov 5 * licensed under MIT license. 6 * 7 * https://github.com/morr/jquery.appear/ 8 * 9 * Version: 0.3.3 10 */ 11(function($) { 12 var selectors = []; 13 14 var check_binded = false; 15 var check_lock = false; 16 var defaults = { 17 interval: 250, 18 force_process: false 19 } 20 var $window = $(window); 21 22 var $prior_appeared; 23 24 function process() { 25 check_lock = false; 26 for (var index = 0; index < selectors.length; index++) { 27 var $appeared = $(selectors[index]).filter(function() { 28 return $(this).is(':appeared'); 29 }); 30 31 $appeared.trigger('appear', [$appeared]); 32 33 if ($prior_appeared) { 34 var $disappeared = $prior_appeared.not($appeared); 35 $disappeared.trigger('disappear', [$disappeared]); 36 } 37 $prior_appeared = $appeared; 38 } 39 } 40 41 // "appeared" custom filter 42 $.expr[':']['appeared'] = function(element) { 43 var $element = $(element); 44 if (!$element.is(':visible')) { 45 return false; 46 } 47 48 var window_left = $window.scrollLeft(); 49 var window_top = $window.scrollTop(); 50 var offset = $element.offset(); 51 var left = offset.left; 52 var top = offset.top; 53 54 if (top + $element.height() >= window_top && 55 top - ($element.data('appear-top-offset') || 0) <= window_top + $window.height() && 56 left + $element.width() >= window_left && 57 left - ($element.data('appear-left-offset') || 0) <= window_left + $window.width()) { 58 return true; 59 } else { 60 return false; 61 } 62 } 63 64 $.fn.extend({ 65 // watching for element's appearance in browser viewport 66 appear: function(options) { 67 var opts = $.extend({}, defaults, options || {}); 68 var selector = this.selector || this; 69 if (!check_binded) { 70 var on_check = function() { 71 if (check_lock) { 72 return; 73 } 74 check_lock = true; 75 76 setTimeout(process, opts.interval); 77 }; 78 79 $(window).scroll(on_check).resize(on_check); 80 check_binded = true; 81 } 82 83 if (opts.force_process) { 84 setTimeout(process, opts.interval); 85 } 86 selectors.push(selector); 87 return $(selector); 88 } 89 }); 90 91 $.extend({ 92 // force elements's appearance check 93 force_appear: function() { 94 if (check_binded) { 95 process(); 96 return true; 97 }; 98 return false; 99 } 100 }); 101})(jQuery); 102 103 104(function($) { 105 $.fn.appear1 = function(fn, options) { 106 107 var settings = $.extend({ 108 109 //arbitrary data to pass to fn 110 data: undefined, 111 112 //call fn only on the first appear? 113 one: true, 114 115 // X & Y accuracy 116 accX: 0, 117 accY: 0 118 119 }, options); 120 121 return this.each(function() { 122 123 var t = $(this); 124 125 //whether the element is currently visible 126 t.appeared = false; 127 128 if (!fn) { 129 130 //trigger the custom event 131 t.trigger('appear1', settings.data); 132 return; 133 } 134 135 var w = $(window); 136 137 //fires the appear event when appropriate 138 var check = function() { 139 140 //is the element hidden? 141 if (!t.is(':visible')) { 142 143 //it became hidden 144 t.appeared = false; 145 return; 146 } 147 148 //is the element inside the visible window? 149 var a = w.scrollLeft(); 150 var b = w.scrollTop(); 151 var o = t.offset(); 152 var x = o.left; 153 var y = o.top; 154 155 var ax = settings.accX; 156 var ay = settings.accY; 157 var th = t.height(); 158 var wh = w.height(); 159 var tw = t.width(); 160 var ww = w.width(); 161 162 if (y + th + ay >= b && 163 y <= b + wh + ay && 164 x + tw + ax >= a && 165 x <= a + ww + ax) { 166 167 //trigger the custom event 168 if (!t.appeared) t.trigger('appear1', settings.data); 169 170 } else { 171 172 //it scrolled out of view 173 t.appeared = false; 174 } 175 }; 176 177 //create a modified fn with some additional logic 178 var modifiedFn = function() { 179 180 //mark the element as visible 181 t.appeared = true; 182 183 //is this supposed to happen only once? 184 if (settings.one) { 185 186 //remove the check 187 w.unbind('scroll', check); 188 var i = $.inArray(check, $.fn.appear.checks); 189 if (i >= 0) $.fn.appear.checks.splice(i, 1); 190 } 191 192 //trigger the original fn 193 fn.apply(this, arguments); 194 }; 195 196 //bind the modified fn to the element 197 if (settings.one) t.one('appear1', settings.data, modifiedFn); 198 else t.bind('appear1', settings.data, modifiedFn); 199 200 //check whenever the window scrolls 201 w.scroll(check); 202 203 //check whenever the dom changes 204 $.fn.appear1.checks.push(check); 205 206 //check now 207 (check)();
208 }); 209 }; 210 211 //keep a queue of appearance checks 212 $.extend($.fn.appear1, { 213 214 checks: [], 215 timeout: null, 216 217 //process the queue 218 checkAll: function() { 219 var length = $.fn.appear1.checks.length; 220 if (length > 0) while (length--) ($.fn.appear1.checks[length])(); 221 }, 222 223 //check the queue asynchronously 224 run: function() { 225 if ($.fn.appear1.timeout) clearTimeout($.fn.appear1.timeout); 226 $.fn.appear1.timeout = setTimeout($.fn.appear1.checkAll, 20); 227 } 228 }); 229 230 231 232})(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.