-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjquery-peekaboo.js
More file actions
53 lines (44 loc) · 1.27 KB
/
jquery-peekaboo.js
File metadata and controls
53 lines (44 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*!
* jQuery Peekaboo 0.1.0
*
* @dependency jQuery 1.7+
* @author Uniba Inc.
* @link https://github.com/uniba/jquery-peekaboo
* @license MIT License
*/
(function(window, document, $) {
var $window = $(window)
, els = [];
$.fn.peekaboo = function(options) {
var args = [].slice.call(arguments);
options = options || {};
if (args.length && 'function' === typeof args[0]) {
options = {
onAppear: args[0]
, onDisappear: args[1]
};
}
return this.each(function() {
els.push($(this)
.on('appear', options.onAppear || $.noop)
.on('disappear', options.onDisappear || $.noop)
);
});
};
$window.on('scroll.peekaboo resize.peekaboo', function(e) {
$.each(els, function() {
var $elem = this;
if ($window.scrollTop() < $elem.offset().top + $elem.height()
&& $elem.offset().top < $window.scrollTop() + $window.height()) {
if ('appeared' !== $elem.data('pkb-state')) {
$elem.data('pkb-state', 'appeared').trigger('appear', e);
}
}
else {
if ('disappeared' !== $elem.data('pkb-state')) {
$elem.data('pkb-state', 'disappeared').trigger('disappear', e);
}
}
});
});
}).call(this, window, document, jQuery);