gpt4 book ai didi

javascript - 事件窗口检测

转载 作者:行者123 更新时间:2023-11-28 07:25:27 25 4
gpt4 key购买 nike

我正在检查当前选项卡/窗口是否获得焦点或不执行某些特定任务。当我切换选项卡但打开选项卡并打开软件(音乐)时,以下功能可以正确工作以检测焦点播放器/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>

最佳答案

您可以使用 window.onfocuswindow.onblur 来代替代码墙。所有主要浏览器都支持它,并且可以完美地满足您想要的目的。例如,它会检测您何时更改选项卡,或如您所说打开另一个应用程序。

示例:

window.onfocus = function() {
console.log('Focused');
};

window.onblur = function() {
console.log('Not focused');
};

关于javascript - 事件窗口检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29720294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com