Event for browser tab hidden/shown?

本秂侑毒 提交于 2019-11-30 17:46:20

问题


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.

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.

In case you are wondering, this is what I'm writing: http://panzi.github.com/Browser-Ponies/


回答1:


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.




回答2:


The closest thing I believe you'll find is the top answer here:

Is there a way track the focus on tab with Javascript?




回答3:


Now they have exactly what was needed: https://developer.mozilla.org/en-US/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API



来源:https://stackoverflow.com/questions/7479305/event-for-browser-tab-hidden-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!