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