How to use HTML5's Page Visibility API to achieve focus js events
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() { //暂停动画或视频 };
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 { // 动画视频开始 } });
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); }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
