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