Skip to content

Commit e0a644d

Browse files
committed
refactor: 精简脚本
1 parent 18f1020 commit e0a644d

File tree

1 file changed

+13
-41
lines changed

1 file changed

+13
-41
lines changed
Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isElement } from "./utility.js"
22

3-
export function toggle(options) {
3+
export async function toggle(options) {
44
let el = null;
55
options = options || {};
66
if (options.id) {
@@ -15,54 +15,26 @@ export function toggle(options) {
1515

1616
if (el !== null) {
1717
if (isFullscreen()) {
18-
exit();
18+
await document.exitFullscreen()
1919
}
2020
else {
21-
enterFullscreen(el);
21+
await enterFullscreen(el);
2222
}
2323
}
2424
}
2525

26-
const enterFullscreen = el => {
27-
el.requestFullscreen() || el.webkitRequestFullscreen || el.mozRequestFullScreen || el.msRequestFullscreen
26+
const enterFullscreen = async el => {
27+
await el.requestFullscreen();
2828

29-
// 处理 ESC 按键退出全屏
30-
var handler = setTimeout(() => {
31-
clearTimeout(handler);
32-
33-
const fullscreenCheck = () => {
34-
if (!isFullscreen()) {
35-
el.classList.remove('bb-fs-open');
36-
document.documentElement.classList.remove('bb-fs-open');
37-
}
38-
else {
39-
el.classList.add('bb-fs-open')
40-
requestAnimationFrame(fullscreenCheck);
41-
}
42-
}
43-
requestAnimationFrame(fullscreenCheck);
44-
}, 200);
29+
if (!isFullscreen()) {
30+
el.classList.remove('bb-fs-open');
31+
document.documentElement.classList.remove('bb-fs-open');
32+
}
33+
else {
34+
el.classList.add('bb-fs-open')
35+
}
4536
}
4637

4738
const isFullscreen = () => {
48-
return document.fullscreen ||
49-
document.webkitIsFullScreen ||
50-
document.webkitFullScreen ||
51-
document.mozFullScreen ||
52-
document.msFullScreent
53-
}
54-
55-
const exit = () => {
56-
if (document.exitFullscreen) {
57-
document.exitFullscreen()
58-
}
59-
else if (document.mozCancelFullScreen) {
60-
document.mozCancelFullScreen()
61-
}
62-
else if (document.webkitExitFullscreen) {
63-
document.webkitExitFullscreen()
64-
}
65-
else if (document.msExitFullscreen) {
66-
document.msExitFullscreen()
67-
}
39+
return document.fullscreenElement !== null;
6840
}

0 commit comments

Comments
 (0)