当前位置:网站首页>浏览器窗口切换激活事件 visibilitychange
浏览器窗口切换激活事件 visibilitychange
2022-06-30 17:42:00 【AdleyTales】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
</style>
</head>
<body>
<h1>浏览器窗口切换激活事件</h1>
<div id="app"></div>
<script>
/**
* visibilitychange:当浏览器可见性发生变化的时候,这个事件会被触发
*
* 我们可以在document对象上注册一个监听visibilitychange事件,
* 根据document.hidden或者document.visibilityState属性做一些业务逻辑。
*
* 示例场景
* 客户端内嵌H5页面经常遇到的一个场景:H5页面有一个视频,正在播放时,点击页面其他按钮,
* 进入客户端原生页面,或者打开一个新的webview,这时视频应该暂停播放,返回该页面时,视频继续播放。
*
*/
document.addEventListener('visibilitychange', (ev) => {
// console.log(ev);
console.log(document.hidden, document.visibilityState) // visible hidden
console.log('----active window ------')
if(!document.hidden) {
doSomething()
}
}, false)
function doSomething() {
console.log('do some thing ...')
}
</script>
</body>
</html>
效果

边栏推荐
- SaaS project management system solution for the financial service industry helps enterprises tap a broader growth service space
- 云上“视界” 创新无限 | 2022阿里云直播峰会正式上线
- countdownlatch 和 completableFuture 和 CyclicBarrier
- C WinForm program interface optimization example
- Compilation problems and solutions of teamtalk winclient
- 正则表达式(正则匹配)
- Vulnerability recurrence ----- 38. Thinkphp5 5.0.23 Remote Code Execution Vulnerability
- Deep learning compiler understanding
- TeamTalk WinClient编译问题及解决方案
- What if icloud photos cannot be uploaded or synchronized?
猜你喜欢

Merged binary tree of leetcode

Multipass Chinese document - setting graphical interface

拓维信息使用 Rainbond 的云原生落地实践

iCloud照片无法上传或同步怎么办?

全栈代码测试覆盖率及用例发现系统的建设和实践

Coding officially entered Tencent conference application market!

The easynvr platform equipment channels are all online. What is the reason for the "network request failure" in the operation?

一点比较有意思的模块

Helping the ultimate experience, best practice of volcano engine edge computing

MRO industrial products procurement management system: enable MRO enterprise procurement nodes to build a new digital procurement system
随机推荐
How to do a good job in software system demand research? Seven weapons make it easy for you to do it
使用excel快速生成sql语句
Rhai 脚本引擎的简单应用示例
屏幕显示技术进化史
Pytorch learning (III)
链表中环的入口结点-链表专题
TCP packet sticking problem
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(上)
PHP uses queues to solve maze problems
What if icloud photos cannot be uploaded or synchronized?
Vulnerability recurrence ----- 38. Thinkphp5 5.0.23 Remote Code Execution Vulnerability
slice
大佬们目前flinksql, cdcmysql跟Kafka双流join,结果放到mysql 或者ka
如何利用AI技术优化独立站客服系统?听听专家怎么说!
Sword finger offer 16 Integer power of numeric value
AI chief architect 10-aica-lanxiang, propeller frame design and core technology
Do you really understand the persistence mechanism of redis?
Countdowncatch and completabilefuture and cyclicbarrier
国内离线安装 Chrome 扩展程序的方法总结
教你30分钟快速搭建直播间