This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 293
/
index.js
120 lines (100 loc) · 2.79 KB
/
index.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
(function ($) {
var selectors = [];
var checkBinded = false;
var checkLock = false;
var defaults = {
interval: 250,
force_process: false
};
var $window = $(window);
var $priorAppeared = [];
function isAppeared() {
return $(this).is(':appeared');
}
function isNotTriggered() {
return !$(this).data('_appear_triggered');
}
function process() {
checkLock = false;
for (var index = 0, selectorsLength = selectors.length; index < selectorsLength; index++) {
var $appeared = $(selectors[index]).filter(isAppeared);
$appeared
.filter(isNotTriggered)
.data('_appear_triggered', true)
.trigger('appear', [$appeared]);
if ($priorAppeared[index]) {
var $disappeared = $priorAppeared[index].not($appeared);
$disappeared
.data('_appear_triggered', false)
.trigger('disappear', [$disappeared]);
}
$priorAppeared[index] = $appeared;
}
}
function addSelector(selector) {
selectors.push(selector);
$priorAppeared.push();
}
// ":appeared" custom filter
$.expr.pseudos.appeared = $.expr.createPseudo(function (_arg) {
return function (element) {
var $element = $(element);
if (!$element.is(':visible')) {
return false;
}
var windowLeft = $window.scrollLeft();
var windowTop = $window.scrollTop();
var offset = $element.offset();
var left = offset.left;
var top = offset.top;
if (top + $element.height() >= windowTop &&
top - ($element.data('appear-top-offset') || 0) <= windowTop + $window.height() &&
left + $element.width() >= windowLeft &&
left - ($element.data('appear-left-offset') || 0) <= windowLeft + $window.width()) {
return true;
}
return false;
};
});
$.fn.extend({
// watching for element's appearance in browser viewport
appear: function (selector, options) {
$.appear(this, options);
return this;
}
});
$.extend({
appear: function (selector, options) {
var opts = $.extend({}, defaults, options || {});
if (!checkBinded) {
var onCheck = function () {
if (checkLock) {
return;
}
checkLock = true;
setTimeout(process, opts.interval);
};
$(window).scroll(onCheck).resize(onCheck);
checkBinded = true;
}
if (opts.force_process) {
setTimeout(process, opts.interval);
}
addSelector(selector);
},
// force elements's appearance check
force_appear: function () {
if (checkBinded) {
process();
return true;
}
return false;
}
});
}(function () {
if (typeof module !== 'undefined') {
// Node
return require('jquery');
}
return jQuery;
}()));