Event for browser tab hidden/shown?(隐藏/显示浏览器选项卡的事件?)
问题描述
当浏览器标签失去/获得焦点时是否有任何 DOM 事件?我知道在 window
上有 blur
和 focus
事件,但是当整个浏览器窗口失去焦点时它们也会触发.然后浏览器可能对用户仍然可见.当然,这样的事件将是特定于浏览器的,但没关系.
Is there any DOM event for when the browser tab loses/gains focus? I know there are the blur
and focus
events on window
, but they also fire when the browser window as a whole loses focus. The browser might then be still visible to the user. Of course such an event would be browser specific, but that's ok.
我想要这个的原因是因为我运行的动画可能会消耗相当多的 CPU 时间.当浏览器选项卡不可见时,没有理由继续制作动画.现在我知道现代浏览器会降低背景选项卡的计时器分辨率,但我实际上可以暂停动画,这样就不会消耗任何 CPU 时间.
The reason why I want this is because I run animations that might consume quite some CPU time. When the browser tab is not visible there is no reason to continue animating. Now I know that modern browsers reduce the timer resolution of background tabs, but I could actually pause the animation, so that no CPU time whatsoever is consumed.
如果您想知道,这就是我要写的内容:http://panzi.github.com/Browser-Ponies/
In case you are wondering, this is what I'm writing: http://panzi.github.com/Browser-Ponies/
推荐答案
至少 Google Chrome 支持 webkitvisibilitychange
事件和 document.webkitHidden
属性.请参阅可见性 API.但它似乎只在显示的选项卡更改时触发,而不是在整个窗口最小化时触发.Internet Explorer 似乎还有一个 visibilitychange
事件,但是 文档 没有说明任何内容.
At least Google Chrome supports a webkitvisibilitychange
event and a document.webkitHidden
property. See the visibility API. But it seems only to fire when the shown tab changes, not when the whole window is minimized. There also seems to be a visibilitychange
event for Internet Explorer, but the documentation doesn't say anything about it.
这篇关于隐藏/显示浏览器选项卡的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:隐藏/显示浏览器选项卡的事件?


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