tab-hidden.js
1.08 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
/**
* 判断当前tab页是否可见
*
* @author: liqi <qi.li@yoho.cn>
* @date: 2016/11/26
*/
var state;
var isHidden;
var changeEvent;
function tabIsHidden() {
if (typeof document.hidden !== 'undefined') {
changeEvent = 'visibilitychange';
state = 'visibilityState';
} else if (typeof document.mozHidden !== 'undefined') {
changeEvent = 'mozvisibilitychange';
state = 'mozVisibilityState';
} else if (typeof document.msHidden !== 'undefined') {
changeEvent = 'msvisibilitychange';
state = 'msVisibilityState';
} else if (typeof document.webkitHidden !== 'undefined') {
changeEvent = 'webkitvisibilitychange';
state = 'webkitVisibilityState';
}
return document[state] === 'hidden';
}
function tabVisible(callback) {
isHidden = tabIsHidden();
document.addEventListener(changeEvent, function() {
if (!isHidden) {
if (callback) {
return callback();
}
}
}, false);
}
module.exports = {
tabVisible: tabVisible,
tabIsHidden: tabIsHidden
};