3 * http://code.google.com/p/jquery-appear/
5 * Copyright (c) 2009 Michael Hixson
6 * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
10 $.fn
.appear = function(fn
, options
) {
12 var settings
= $.extend({
14 //arbitrary data to pass to fn
17 //call fn only on the first appear?
22 return this.each(function() {
26 //whether the element is currently visible
31 //trigger the custom event
32 t
.trigger('appear', settings
.data
);
38 //fires the appear event when appropriate
39 var check = function() {
41 //is the element hidden?
42 if (!t
.is(':visible')) {
49 //is the element inside the visible window?
50 var a
= w
.scrollLeft();
51 var b
= w
.scrollTop();
56 if (y
+ t
.height() >= b
&&
57 y
<= b
+ w
.height() &&
61 //trigger the custom event
62 if (!t
.appeared
) t
.trigger('appear', settings
.data
);
66 //it scrolled out of view
71 //create a modified fn with some additional logic
72 var modifiedFn = function() {
74 //mark the element as visible
77 //is this supposed to happen only once?
81 w
.unbind('scroll', check
);
82 var i
= $.inArray(check
, $.fn
.appear
.checks
);
83 if (i
>= 0) $.fn
.appear
.checks
.splice(i
, 1);
86 //trigger the original fn
87 fn
.apply(this, arguments
);
90 //bind the modified fn to the element
91 if (settings
.one
) t
.one('appear', settings
.data
, modifiedFn
);
92 else t
.bind('appear', settings
.data
, modifiedFn
);
94 //check whenever the window scrolls
97 //check whenever the dom changes
98 $.fn
.appear
.checks
.push(check
);
105 //keep a queue of appearance checks
106 $.extend($.fn
.appear
, {
112 checkAll: function() {
113 var length
= $.fn
.appear
.checks
.length
;
114 if (length
> 0) while (length
--) ($.fn
.appear
.checks
[length
])();
117 //check the queue asynchronously
119 if ($.fn
.appear
.timeout
) clearTimeout($.fn
.appear
.timeout
);
120 $.fn
.appear
.timeout
= setTimeout($.fn
.appear
.checkAll
, 20);
124 //run checks when these methods are called
125 $.each(['append', 'prepend', 'after', 'before', 'attr',
126 'removeAttr', 'addClass', 'removeClass', 'toggleClass',
127 'remove', 'css', 'show', 'hide'], function(i
, n
) {
130 $.fn
[n
] = function() {
131 var r
= old
.apply(this, arguments
);