forked from shaunbowe/jquery.visibilityChanged
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisibilityChanged.js
35 lines (32 loc) · 1.11 KB
/
visibilityChanged.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
(function ($) {
var defaults = {
callback: function () { },
runOnLoad: true,
frequency: 100,
previousVisibility : null
};
var methods = {};
methods.checkVisibility = function (element, options) {
if (jQuery.contains(document, element[0])) {
var previousVisibility = options.previousVisibility;
var isVisible = element.is(':visible');
options.previousVisibility = isVisible;
if (previousVisibility == null) {
if (options.runOnLoad) {
options.callback(element, isVisible);
}
} else if (previousVisibility !== isVisible) {
options.callback(element, isVisible);
}
setTimeout(function() {
methods.checkVisibility(element, options);
}, options.frequency);
}
};
$.fn.visibilityChanged = function (options) {
var settings = $.extend({}, defaults, options);
return this.each(function () {
methods.checkVisibility($(this), settings);
});
};
})(jQuery);