对于浏览器窗口是否可见的判断

原创 何时入土 随笔 随笔 135阅读 2018-05-18 16:46:03 举报

// 各种浏览器兼容
var hidden, state, visibilityChange;
if (typeof document.hidden !== "undefined") {
hidden = "hidden";
visibilityChange = "visibilitychange";
state = "visibilityState";
} else if (typeof document.mozHidden !== "undefined") {
hidden = "mozHidden";
visibilityChange = "mozvisibilitychange";
state = "mozVisibilityState";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
state = "msVisibilityState";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
state = "webkitVisibilityState";
}

// 添加监听器,在title里显示状态变化
document.addEventListener(visibilityChange, function() {
    document.title = document[state];
}, false);
// 初始化
document.title = document[state];
评论 ( 0 )
最新评论
暂无评论

赶紧努力消灭 0 回复