javascript - 检测您的网站是否在后台运行

标签 javascript jquery

有没有办法检测我的网站是否在后台运行, 例如:我的网站在浏览器中打开,但用户站在另一个选项卡上 例如 YouTube。

最佳答案

ifvisible.js , demo

Crossbrowser & lightweight way to check if user is looking at the page or interacting with it.

使用Page Visibility API , demo

The Page Visibility API lets you know when a webpage is visible or in focus. With tabbed browsing, there is a reasonable chance that any given webpage is in the background and thus not visible to the user. When the user minimizes the webpage or moves to another tab, the API sends a visibilitychange event regarding the visibility of the page. You can detect the event and perform some actions or behave differently. For example, if your web app is playing a video, it would pause the moment the user looks at another browser, and plays again when the user returns to the tab. The user does not lose their place in the video and can continue watching.

关于javascript - 检测您的网站是否在后台运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32092246/

相关文章:

javascript - JavaScript 中的 SonarQube 正则表达式漏洞问题

jQuery - 遍历 xml 并创建它的列表

jquery - 为什么更改 css 光标属性会阻止我的 ajax 帖子工作?

javascript - 我如何使用自己的 JavaScript 库处理 "think in QUnit"?

javascript - 如何通过 ul 中的索引将类设置为 li 标签

jquery - DOM更新后使用draggable

javascript - 编写一个函数,该函数接受一个对象数组以及一个具有属性和值的对象

javascript - D3 - 以点击的国家为中心

javascript - 单击时使用 javascript 添加内容

javascript with() 函数在 Opera、Chrome 或 Brave 中不起作用