页面激活判断

第一种:js版

//页面激活判断
var hiddenProperty = 'hidden' in document ? 'hidden' :    
    'webkitHidden' in document ? 'webkitHidden' :    
    'mozHidden' in document ? 'mozHidden' :    
    null;
var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
var onVisibilityChange = function(){
    if (!document[hiddenProperty]) {          
       alert('页面非激活');
    }else{
        alert('页面激活');
    }
}
document.addEventListener(visibilityChangeEvent, onVisibilityChange);

第二种:

ifvisible.js

ifvisible.js提供跨浏览器和轻量级的方式来检查用户检查用户是否在看页面还是与页面交互。

它可以处理活动状态,如在页面上是空闲或活跃。

ifvisible.on("blur", function(){
    // example code here..
    animations.pause();
});

ifvisible.on("focus", function(){
    // resume all animations
    animations.resume();
});

项目主页:http://serkanyersen.github.io/ifvisible.js/

原文地址:https://www.cnblogs.com/shihaiming/p/6277160.html