百度知道原回答:https://zhidao.baidu.com/question/541794991.htmlhtml
能夠經過document.hidden屬性判斷當前頁面是不是激活狀態。web
兼容性:IE10+,Firefox10+,Chrome14+,Opera12.1+,Safari7.1+spa
兼容性寫法示例:code
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);