首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

怎么判断当前窗口是否激活

2013-10-31 
如何判断当前窗口是否激活http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-br

如何判断当前窗口是否激活
http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active

Current browser support:Chrome 13Internet Explorer 10Firefox 10Opera 12.10 [read notes]The following code makes use of the API, falling back to the less reliable blur/focus method in incompatible browsers.(function() {    var hidden = "hidden";    // Standards:    if (hidden in document)        document.addEventListener("visibilitychange", onchange);    else if ((hidden = "mozHidden") in document)        document.addEventListener("mozvisibilitychange", onchange);    else if ((hidden = "webkitHidden") in document)        document.addEventListener("webkitvisibilitychange", onchange);    else if ((hidden = "msHidden") in document)        document.addEventListener("msvisibilitychange", onchange);    // IE 9 and lower:    else if ('onfocusin' in document)        document.onfocusin = document.onfocusout = onchange;    // All others:    else        window.onpageshow = window.onpagehide             = window.onfocus = window.onblur = onchange;    function onchange (evt) {        var v = 'visible', h = 'hidden',            evtMap = {                 focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h             };        evt = evt || window.event;        if (evt.type in evtMap)            document.body.className = evtMap[evt.type];        else                    document.body.className = this[hidden] ? "hidden" : "visible";    }})();


也可以监听鼠标移动事件,若移动则说明在当前窗口。若超过几分钟没动,则说明可能不在当前状态。但是性能上来说肯定是前一种方案更好。

热点排行