2011-05-27 97 views

回答

3

参见:http://www.thefutureoftheweb.com/blog/detect-browser-window-focus

这些事件在每一个主要 浏览器(Firefox,Internet Explorer的 6/7,Safari浏览器& Opera)的工作。

演示:http://www.thefutureoftheweb.com/demo/2007-05-16-detect-browser-window-focus/

function onBlur() { 
    document.body.className = 'blurred'; 
}; 
function onFocus(){ 
    document.body.className = 'focused'; 
}; 

if (/*@[email protected]*/false) { // check for Internet Explorer 
    document.onfocusin = onFocus; 
    document.onfocusout = onBlur; 
} else { 
    window.onfocus = onFocus; 
    window.onblur = onBlur; 
} 
+0

你会认为会有一些可以做到的事情,有滚动条,为什么不是其他的东西?但感谢您的答案。 – penguinrob 2011-05-27 23:02:30

+1

我在检查Google后更改了我的答案。事实证明,毕竟有一种方法。对于那个很抱歉。 – thirtydot 2011-05-27 23:02:55

+0

Schweet!谢谢,男人! – penguinrob 2011-05-27 23:07:44

0

不幸:window-inactive不规范并且仅适用于使用::selection据我所知滚动条和文本选择。你必须使用JavaScript来获得你想要的。

+1

谢谢,我认为onBlur和onFocus会为我工作。 – penguinrob 2011-05-27 23:10:13

2

由于您只关注WebKit,因此您可能会使用:window-inactive伪选择器,该选择器应该适用于滚动条。我没有在MacOS X上测试过它,但你可以试试它。

但是,如果您想要更多的跨浏览器,请使用JavaScript根据窗口活动定义CSS类。看到这个帖子:Is there a way to detect if a browser window is not currently active?

+1

我没有注意到你已经链接的问题。它的答案和我在这里的回答完全一样。好吧。 – thirtydot 2011-05-27 23:16:30

相关问题