visibilitychangeEvent
Examples
Execute a piece of JavaScript code when the content of its tab becomes visible or hidden:
Try it Yourself »
Definition and Usage
The visibilitychange event is an event triggered on the Document object in the browser, used to detect whether the visibility state of the current page has changed.
When the user switches to another tab, minimizes the browser window, or hides the browser window behind other windows, the visibility of the page changes, and the visibilitychange event is triggered at this time.
Syntax
document.addEventListener('visibilitychange', function() {
if (document.visibilityState === 'visible') {
// 页面变为可见状态时执行的操作
} else if (document.visibilityState === 'hidden') {
// 页面变为不可见状态时执行的操作
}
});
Browser Support
| Event | |||||
|---|---|---|---|---|---|
| ondrop | 62 | 18 | 56 | 14.1 | 49 |
Note:visibilitychangeThe behavior of the event may differ in different browsers, so browser compatibility needs to be considered when using it. In addition, power-saving modes on some mobile devices or computers may also affect the visibility state of the page, thereby triggering this event.
Event Object Other Extensions