当前位置:网站首页>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>
边栏推荐
- [C Advanced] brother Peng takes you to play with strings and memory functions
- 小米青年工程师,本来只是去打个酱油
- Competition and adventure burr
- SAML2.0 笔记(一)
- Verilog wire type
- [golang] leetcode intermediate bracket generation & Full Permutation
- Work hard all day long and be alert at sunset
- 终日乾乾,夕惕若厉
- 2022-2028 global soft capsule manufacturing machine industry research and trend analysis report
- 使用 useDeferredValue 进行异步渲染
猜你喜欢

Mongodb non relational database

JS <2>

Mongodb base de données non relationnelle

West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials

Verilog 时序控制

GB/T-2423.xx 环境试验文件,整理包括了最新的文件里面

Verilog avoid latch

buu_ re_ crackMe

MySQL advanced (Advanced) SQL statement (II)

浅谈线程池相关配置
随机推荐
Framing in data transmission
初出茅庐市值1亿美金的监控产品Sentry体验与架构
Delphi xe10.4 installing alphacontrols15.12
Continuous assignment of Verilog procedure
Qualcomm platform WiFi -- Native crash caused by WiFi
Xiaomi, a young engineer, was just going to make soy sauce
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
Gradle notes
In depth interpretation of pytest official documents (26) customized pytest assertion error information
Docker installs canal and MySQL for simple testing and implementation of redis and MySQL cache consistency
Qualcomm platform wifi-- WPA_ supplicant issue
MySQL advanced (Advanced) SQL statement (II)
ZABBIX API creates hosts in batches according to the host information in Excel files
Discussion on related configuration of thread pool
Verilog state machine
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
Unit · elementary C # learning notes
Golang configure export goprivate to pull private library code
Verilog 状态机
ORA-01547、ORA-01194、ORA-01110