forked from morr/jquery.appear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.appear.js
91 lines (83 loc) · 2 KB
/
jquery.appear.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* jQuery appear plugin
*
* Copyright (c) 2012 Andrey Sidorov
* licensed under MIT license.
*
* https://github.com/morr/jquery.appear/
*
* Version: 0.1
*/
(function($) {
var data = {};
var check_binded = false;
var check_lock = false;
var defaults = {
interval: 250
}
var $window = $(window);
var $document = $(document);
function check_appearance(element) {
var $element = $(element);
if (!$element.is(':visible')) {
return false;
}
var window_left = $window.scrollLeft();
var window_top = $window.scrollTop();
var offset = $element.offset();
var left = offset.left;
var top = offset.top;
if (top + $element.height() >= window_top &&
top <= window_top + $window.height() &&
left + $element.width() >= window_left &&
left <= window_left + $window.width()) {
return true;
} else {
return false;
}
}
function process() {
check_lock = false;
for (var selector in data) {
var $appeared = $(_.select($(selector), function(v,k) {
return check_appearance(v);
}));
if ($appeared.length) {
if (data[selector]) {
data[selector]($appeared);
} else {
$appeared.trigger('appear', $appeared);
}
}
}
}
$.fn.extend({
// watching for element's appearance in browser viewport
appear: function(callback, options) {
$.extend(defaults, options || {});
if (!check_binded) {
$(window).scroll(function() {
if (check_lock) {
return;
}
check_lock = true;
_.delay(process, defaults.interval);
});
check_binded = true;
}
_.delay(process, defaults.interval);
data[this.selector] = callback;
return $(this.selector);
}
});
$.extend({
// force elements's appearance check
force_appear: function() {
if (check_binded) {
process();
return true;
};
return false;
}
});
})(jQuery);