Home > Web Front-end > H5 Tutorial > body text

How to use HTML5's Page Visibility API to achieve focus js events

伊谢尔伦
Release: 2017-05-30 10:06:10
Original
2044 people have browsed it

The current window gets focus js event. Before HTML5 was released, we used window.onfocus and window.onblur to gain and lose window focus.

//当前窗口得到焦点 
window.onfocus = function() { 
  //播放动画或视频 
}; 
 
//当前窗口失去焦点 
window.onblur = function() { 
  //暂停动画或视频 
};
Copy after login

This method can realize switching tags and pausing animated videos, but it will cause Let me have a question. Since it is the focus of judgment, if a small window is placed on the current page, the animation will be paused on the current page. Just imagine, if you open a chat window to chat with MM while watching a movie, when you operate the chat When the window is opened, the video is paused, which is not the effect you want.

Now let’s see how HTML5 solves it. H5 provides many simple and practical APIs, and Page Visibility API is one of them. Page Visibility API can effectively help us complete this judgment.

Use the Page Visibility API of html5 to implement

The API itself is very simple and consists of the following three parts.

document.hidden: A Boolean value indicating whether the page is hidden. Hiding the page includes the page being in a background tab or minimizing the browser (note that a page covered by other software does not count as hidden, such as an open sublime covering the browser).

document.visibilityState: Value representing the following 4 possible states

hidden: the page is in the background tab or the browser is minimized

visible: the page is in the foreground tab Medium

prerender: The page performs pre-rendering processing outside the screen. The value of document.hidden is true

unloaded: The page is being unloaded from memory

Visibilitychange event: When the document is unloaded from memory This event is triggered when visible becomes invisible or from invisible to visible.

In this way, we can listen to the Visibilitychange event. When the event is triggered, get the value of document.hidden and process some events on the page based on this value.

document.addEventListener('visibilitychange', function() { 
  var isHidden = document.hidden; 
  if (isHidden) { 
    // 动画视频暂停 
  } else { 
    // 动画视频开始 
  } 
});
Copy after login

Combined with the examples in the demo, when switching tabs or minimizing, the video in the demo will be paused. When the current page is restored, the video in the demo will continue to play. The complete code is as follows:

var videoElement = document.getElementById("videoElement"); 
 
// 如果页面被隐藏,则暂停播放,如果页面恢复,则继续播放 
function handleVisibilityChange() { 
  if (document[hidden]) { 
    videoElement.pause(); 
  } else { 
    videoElement.play(); 
  } 
} 
 
// 判断浏览器的支持情况 
if (typeof document.addEventListener === "undefined" || typeof document[hidden] === "undefined") { 
  consol.log("This demo requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API."); 
} else { 
  // 监听visibilityChange事件    
  document.addEventListener(visibilityChange, handleVisibilityChange, false); 
     
  // 当播放器暂停的时候,将页面标题设置为:Paused. 
  videoElement.addEventListener("pause", function(){ 
    document.title = 'Paused'; 
  }, false); 
     
  // 当播放器正常播放时,将页面标题设置为:Playing. 
  videoElement.addEventListener("play", function(){ 
    document.title = 'Playing';  
  }, false); 
}
Copy after login


The above is the detailed content of How to use HTML5's Page Visibility API to achieve focus js events. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!