JS 判断当前页面是否被激活

visibilitychange:当前的活动状态事件
document.hidden:判断当前页面有没有被隐藏 (是否是激活状态)
兼容性:IE10+,Firefox10+,Chrome14+,Opera12.1+,Safari7.1+

例;:判断当前网页有没有被激活

 //处理兼容
var bowhidden="hidden" in document?"hidden": "webkithidden" in document?"webkithidden": "mozhidden" in document ?"mozhidden": null;
var vibchage="visibilitychange" || "webkitvisibilitychange" || "mozvisibilitychange";
document.addEventListener(vibchage,function (){
        /*ie10+  moz  webkit  默认*/
        if(!document[bowhidden]) /*false*/
        {
            console.log("激活");
        }
        else{
            /*true*/
            console.log("隐藏");
        }
});

你可能感兴趣的:(js)