Is there any way to detect that window is currently active in IE8?(有什么方法可以检测到该窗口当前在 IE8 中处于活动状态吗?)
问题描述
有什么方法可以在 IE8 中检测窗口当前处于活动状态(显示在活动选项卡/窗口上)?
Is there any way to detect that the window is currently active (is being shown on active tab/window) in IE8?
我知道有像 onfocusin
/onfocus
这样的事件 - 但这不是一个完美的解决方案,因为窗口还必须接收焦点才能触发事件 - 所以当用户只是切换选项卡而不接触窗口本身时,这不起作用.
I know there are events like onfocusin
/onfocus
- but this is not a perfect solution, since the window must also receive focus for the event to be fired - so this does not work when the user just switches the tabs without touching the window itself.
我相信对于这种普通的用例,必须有一些简单、优雅的解决方案.
I believe there has to be some simple, elegant solution for such ordinary use-case.
推荐答案
我写了一个 jQuery 插件来做这个:http://mths.be/visibility 它为您提供了一个非常简单的 API,允许您在页面的可见性状态发生变化时执行回调.
I’ve written a jQuery plugin that does this: http://mths.be/visibility It gives you a very simple API that allows you to execute callbacks when the page’s visibility state changes.
它通过使用 Page Visibility API 来实现a> 在支持它的地方,并在旧浏览器中回退到良好的旧 focus
和 blur
.
演示: http://mathiasbynens.be/demo/jquery-visibility
这个插件只提供了两个自定义事件供您使用:show
和 hide
.当页面可见性状态发生变化时,会触发相应的事件.
This plugin simply provides two custom events for you to use: show
and hide
. When the page visibility state changes, the appropriate event will be triggered.
您可以单独使用它们:
$(document).on('show', function() {
// the page gained visibility
});
……和……
$(document).on('hide', function() {
// the page was hidden
});
由于大多数情况下您都需要这两个事件,因此最好的选择是使用事件地图.这样,您可以一次性绑定两个事件处理程序:
Since most of the time you’ll need both events, your best option is to use an events map. This way, you can bind both event handlers in one go:
$(document).on({
'show': function() {
console.log('The page gained visibility; the `show` event was triggered.');
},
'hide': function() {
console.log('The page lost visibility; the `hide` event was triggered.');
}
});
插件将检测浏览器是否原生支持页面可见性 API,并在 中将此信息公开为布尔值 (
:true
/false
)>$.support.pageVisibility
The plugin will detect if the Page Visibility API is natively supported in the browser or not, and expose this information as a boolean (true
/false
) in $.support.pageVisibility
:
if ($.support.pageVisibility) {
// Page Visibility is natively supported in this browser
}
这篇关于有什么方法可以检测到该窗口当前在 IE8 中处于活动状态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有什么方法可以检测到该窗口当前在 IE8 中处于活


- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01