Chrome应用窗口最初没有针对性。当您单击窗口区域时,它将获得焦点。但是,此时不会捕获鼠标单击事件。我想知道如何在窗口获得焦点时捕获鼠标事件。我的意思是只是鼠标点击事件,它将窗口从背景带到前台。
答案 0 :(得分:0)
将window.onfocus事件与click事件结合使用。
这里你去:jsfiddle.net/MarcGuiselin/7vyey4Ld/1
var justfocused=false;//true for 500ms after the page was focused
document.body.onclick=function(e){//onclick event always occurs after onfocus event
if(justfocused){//if our window was just focused
justfocusedevent(e);
justfocused=false;//user has to refocus this tab for this function to run again
}else{
console.log("just a regular click");
}
}
window.onfocus=function(){
justfocused=true;
setTimeout(function(){justfocused=false;},500);//reset just in case
console.log("window focused");
}
function justfocusedevent(e){
console.log("we just focused on this window at: ("+e.clientX+","+e.clientY+")");
}
尝试多次单击红色结果页面,然后单击其他位置以取消聚焦结果窗口并再次单击它。