当前位置:网站首页>h5中的页面显示隐藏执行事件
h5中的页面显示隐藏执行事件
2022-07-02 03:20:00 【75624839】
visibilitychange事件
是浏览器的新事件方法,他主要是用来监听页面是否处于显示状态,现在主流的浏览器都支持此事件,当浏览器进行切换选项卡,最小化,最大化等操作的时候会主动触发该事件。
document.hidden属性
用来表示当前页面是隐藏状态还是激活显示状态,当页面是隐藏非激活状态的时候该值为true,当页面是激活状态的时候该值为false,该属性为只读属性。
我们可以通过上边的visibilitychange事件结合document.hidden做一些效果
一个带背景音乐的页面:
<!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.hidden</title>
</head>
<body>
<!-- 创建一个播放器 -->
<audio id="audioDom" src="https://aimg8.oss-cn-shanghai.aliyuncs.com/VUEAdmin/gameActivity/DaZhuanPan/back_music.mp3"></audio>
<!-- 一个播放按钮 -->
<button id="btn">播放</button>
<script> // 获取页面中的audio播放器Dom元素 var audioDom = document.getElementById('audioDom'); // 给按钮绑定事件单击的时候进行音乐播放 document.getElementById('btn').addEventListener('click', function(){
audioDom.play(); }); // 给document绑定一个visibilitychange事件,当前页面显示或者隐藏的时候执行该事件 document.addEventListener("visibilitychange", function() {
// 判断当前document是显示状态还是隐藏状态 if(!document.hidden){
// 如果是显示状态执行音乐播放操作 audioDom.play(); }else{
// 如果是隐藏状态执行音乐暂停操作 audioDom.pause(); } }); </script>
</body>
</html>
边栏推荐
- 自定义组件的 v-model
- Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts
- Framing in data transmission
- MSI announced that its motherboard products will cancel all paper accessories
- Start a business
- What is the binding path of SAP ui5
- Download and use of the super perfect screenshot tool snipaste
- Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
- A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high
- Grpc quick practice
猜你喜欢
[HCIA continuous update] overview of dynamic routing protocol
Start a business
C reflection practice
How to develop digital collections? How to develop your own digital collections
How to create an instance of the control defined in SAP ui5 XML view at runtime?
Render header usage of El table
uniapp 使用canvas 生成海报并保存到本地
C shallow copy and deep copy
Continuous assignment of Verilog procedure
2022-2028 global manual dental cleaning equipment industry research and trend analysis report
随机推荐
GSE104154_ scRNA-seq_ fibrotic MC_ bleomycin/normalized AM3
[HCIA continuous update] overview of dynamic routing protocol
On redis (II) -- cluster version
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high
Aaaaaaaaaaaa
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
Verilog 过程赋值 区别 详解
Qualcomm platform WiFi -- Native crash caused by WiFi
4. Find the median of two positive arrays
Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts
Grpc quick practice
Design details of SAP e-commerce cloud footernavigationcomponent
命名块 verilog
跟着CTF-wiki学pwn——ret2shellcode
[HCIA continuous update] working principle of OSPF Protocol
Cache processing scheme in high concurrency scenario
只需简单几步 - 开始玩耍微信小程序
2022-2028 global encryption software industry research and trend analysis report
Detailed explanation of the difference between Verilog process assignment