js 浏览器页面切换事件

document.addEventListener('visibilitychange', function() {
console.log(isHidden() + "-" + new Date().toLocaleTimeString())
});

function getHiddenProp() {
var prefixes = ['webkit', 'moz', 'ms', 'o'];

// if 'hidden' is natively supported just return it
if('hidden' in document) return 'hidden';

// otherwise loop over all the known prefixes until we find one
for(var i = 0; i < prefixes.length; i++) {
if((prefixes[i] + 'Hidden') in document)
return prefixes[i] + 'Hidden';
}

// otherwise it's not supported
return null;
}

function isHidden() {

var prop = getHiddenProp();
if(!prop) return false;

return document[prop];
}

原文地址:https://www.cnblogs.com/lgjc/p/7125883.html