forked from morr/jquery.appear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.appear.js
149 lines (129 loc) · 3.69 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* jQuery appear plugin
*
* Copyright (c) 2012 Andrey Sidorov
* licensed under MIT license.
*
* https://github.com/morr/jquery.appear/
*
* Version: 0.3.7
*/
(function($) {
var $window = $(window);
var selectors = [];
var privor_visibles = [];
var defaults = {
interval: 250,
force_process: false
};
var throttle_interval = 250
function process () {
selectors.forEach(function (selector) {
$(selector).each(function () {
var element = this
var was_visible = $.inArray(element, privor_visibles) !== -1
var is_visible = $(element).is(':appeared')
if (was_visible !== is_visible) {
// trigger events
$(element).trigger(is_visible ? 'appear' : 'disappear', [$(element)])
// add / remove elements from privor_visibles
if (is_visible) {
privor_visibles.push(element)
} else {
privor_visibles = privor_visibles.filter(function (prior_visible) {
return prior_visible !== element
})
}
}
})
})
}
var throttled_process = throttle(process, throttle_interval)
function throttle (callback, limit) {
var wait = false; // Initially, we're not waiting
return function () { // We return a throttled function
if (!wait) { // If we're not waiting
callback.call(); // Execute users function
wait = true; // Prevent future invocations
setTimeout(function () { // After a period of time
wait = false; // And allow future invocations
}, limit);
}
}
}
// "appeared" custom filter
$.expr[':']['appeared'] = function(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;
return top + $element.height() >= window_top &&
top - ($element.data('appear-top-offset') || 0) <= window_top + $window.height() &&
left + $element.width() >= window_left &&
left - ($element.data('appear-left-offset') || 0) <= window_left + $window.width()
};
$.extend({
// watching for element's appearance in browser viewport
appear: function(selector, options) {
var opts = $.extend({}, defaults, options || {});
if ($.inArray(selectors, selector) === -1) {
selectors.push(selector);
}
once(function () {
$(window).scroll(throttled_process).resize(throttled_process);
})()
if (opts.force_process) {
throttled_process();
}
return $(selector);
}
});
$.extend({
// force elements's appearance check
force_appear: function() {
var hasSelectors = !!selectors.length
if (hasSelectors) {
process();
}
return hasSelectors;
},
set_appear_throttle_interval: function (interval) {
throttle_interval = interval
},
});
$.extend({
appear_privor_visibles: function () {
return privor_visibles
}
})
var once = function (fn) {
var called = false;
return function () {
if (called) {
return
}
called = true;
fn();
}
}
// garbage collection every 30mins
setInterval(function () {
privor_visibles.forEach(function (element, index) {
if (!$.contains(document.body, element)) {
privor_visibles.splice(index, 1)
}
})
}, 1000 * 60 * 30)
})(function() {
if (typeof module !== 'undefined') {
// Node
return require('jquery');
} else {
return jQuery;
}
}());