检测选项卡是否处于活动状态? (与焦点不同)
我的 Javascript 使用计时器来刷新页面的 CSS。我喜欢让它在 Safari 在后台运行并且我正在代码编辑器中进行更改时运行,但我不希望它在不在活动浏览器选项卡中时运行。
window.blur
和 window.focus
在另一个选项卡处于焦点时被触发,但当选项卡处于活动状态且 Safari 处于后台时也会被触发,因此它们不会不帮我这里。
如何单独检测运行 Javascript 的选项卡当前是否处于活动状态?
My Javascript uses a timer to refresh the CSS of a page. I like to have it run when Safari is in the background and I'm making changes in my code editor, but I don't want it to run when it's not in the active browser tab.
window.blur
and window.focus
get triggered when another tab is in focus, but they also get triggered when the tab is active and Safari is in the background, so they don't help me here.
How do I detect solely if the tab my Javascript is running in is currently active?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
W3C 有一个工作草案来跟踪这种具体情况:页面可见性 API 。 Chrome 似乎是唯一支持该功能的浏览器,目前有实验性 API 。您可以使用 Web Inspector 控制台中的这个小功能进行尝试:
在使用它之后,我认为这正是我所需要的。基本上,只有当文档位于其窗口的活动选项卡中时, document.webkitHidden 才为 false,无论其他窗口或应用程序是否处于活动状态。
The W3C has a working draft for tracking this exact situation: the page visibility API. It looks like Chrome is the only browser that supports it, currently with an experimental API. You can try it out with this little function in the Web Inspector console:
After playing around with it, I think this is exactly what I need. Basically document.webkitHidden is only false if the document is in the active tab of its window, regardless of whether other windows or applications are active.
这里有一些文档: https://developer.apple.com/documentation/safariextensions
There is some documentation here: https://developer.apple.com/documentation/safariextensions
尝试类似 onmouseover 的东西..
try something like onmouseover..