JavaScript中的document.onfullscreenchange


本文作者: jsweibo

本文链接: https://jsweibo.github.io/2019/11/14/JavaScript%E4%B8%AD%E7%9A%84document-onfullscreenchange/

摘要

本文主要讲述了:

  1. 触发时机

正文

触发时机

当浏览器切入或者切出全屏模式时,将会触发:

  1. 目标元素的fullscreenchange事件
  2. 包含目标元素的documentfullscreenchange事件

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<button id="foo" type="button">Fullscreen Display</button>
<script>
const foo = document.querySelector('#foo');

foo.addEventListener('click', function (event) {
if (document.fullscreenElement === null) {
this.requestFullscreen();
} else {
document.exitFullscreen();
}
});

foo.addEventListener('fullscreenchange', function (event) {
console.log(event);
});

document.addEventListener('fullscreenchange', function (event) {
console.log(event);
});
</script>
</body>
</html>

参考资料

本文作者: jsweibo

本文链接: https://jsweibo.github.io/2019/11/14/JavaScript%E4%B8%AD%E7%9A%84document-onfullscreenchange/


本文对你有帮助?请支持我


支付宝
微信