百度知道原回答:https://zhidao.baidu.com/question/541794991.html
可以通过document.hidden属性判断当前页面是否是激活状态。
兼容性:IE10+,Firefox10+,Chrome14+,Opera12.1+,Safari7.1+
兼容性写法示例:
1 var hiddenProperty = ‘hidden‘ in document ? ‘hidden‘ : 2 ‘webkitHidden‘ in document ? ‘webkitHidden‘ : 3 ‘mozHidden‘ in document ? ‘mozHidden‘ : 4 null; 5 var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, ‘visibilitychange‘); 6 var onVisibilityChange = function(){ 7 if (!document[hiddenProperty]) { 8 console.log(‘页面非激活‘); 9 }else{ 10 console.log(‘页面激活‘) 11 } 12 } 13 document.addEventListener(visibilityChangeEvent, onVisibilityChange);
原文:https://www.cnblogs.com/TbKing-blogs/p/11213715.html