我正在检查当前标签/窗口是否聚焦以执行某些特定任务。以下功能正在用于检测焦点,当我切换标签但是当标签打开并且我打开软件时(音乐)播放器/ Windows文件夹)保持标签处于活动/打开状态,该功能仍然认为窗口/标签是聚焦的!我试图实现的情况是检测窗口/标签因打开应用程序/软件而失去焦点目前专注于窗口/标签。如果你根据我的代码提供一个jsfiddle你的答案将会很棒!
$(document).ready(function() {
var hidden, change, vis = {
hidden: "visibilitychange",
mozHidden: "mozvisibilitychange",
webkitHidden: "webkitvisibilitychange",
msHidden: "msvisibilitychange",
oHidden: "ovisibilitychange" /* not currently supported */
};
for (hidden in vis) {
if (vis.hasOwnProperty(hidden) && hidden in document) {
change = vis[hidden];
break;
}
}
if (change)
document.addEventListener(change, onchange);
else if (/*@cc_on!@*/false) // IE 9 and lower
document.onfocusin = document.onfocusout = onchange
else
window.onfocus = window.onblur = onchange;
function onchange (evt) {
evt = evt || window.event;
if (evt.type == "focus" || evt.type == "focusin")
window_focus = true;
else if (evt.type == "blur" || evt.type == "focusout")
window_focus = false;
else
window_focus = this[hidden] ? false : true;
}
});
框:
<frameset rows="130,*" style="border: 1px #CC3333" noresize="noresize" ">
<frame name="showcountframe" src="https://jsfiddle.net/nvobhaor/1/" scrolling=no marginheight="2" marginwidth="2" noresize="noresize">
<frame name="showcontframe" src="showcontframe.html" marginheight="0" marginwidth="0" noresize="noresize">
</frameset><noframes></noframes>
答案 0 :(得分:0)
您可以使用window.onfocus
和window.onblur
代替那段代码。它受到所有主流浏览器的支持,可以完美地满足您的需求。例如,它会检测您更改选项卡的时间,或者按照您的说法打开另一个应用程序。
示例:强>
window.onfocus = function() {
console.log('Focused');
};
window.onblur = function() {
console.log('Not focused');
};