📌  相关文章
📜  javascript 如果浏览器失焦 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:44.645000             🧑  作者: Mango

代码示例1
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
  hidden = "hidden";
  visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
  hidden = "msHidden";
  visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
  hidden = "webkitHidden";
  visibilityChange = "webkitvisibilitychange";
}
function handleVisibilityChange() {
  if (document[hidden]) {
      // Do here something if page is hidden (not in focus)
  } else {
    // Do here something if page is in focus
  }
}
if (typeof document.addEventListener === "undefined" || hidden === undefined) { // Skip if the browser doesn't support addEventListener or the Page Visibility API
} else {
  document.addEventListener(visibilityChange, handleVisibilityChange, false); // Handle page visibility change
}