隐藏/显示浏览器选项卡的事件? [英] Event for browser tab hidden/shown?

查看:21
本文介绍了隐藏/显示浏览器选项卡的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当浏览器标签失去/获得焦点时是否有任何 DOM 事件?我知道在 window 上有 blurfocus 事件,但是当整个浏览器窗口失去焦点时它们也会触发.然后浏览器可能对用户仍然可见.当然,这样的事件将是特定于浏览器的,但没关系.

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.

这篇关于隐藏/显示浏览器选项卡的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆