-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (61 loc) · 1.52 KB
/
index.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class FullScreen {
constructor(el, options = {}) {
const { onChange, onEnter, onExit } = options;
this.el = el;
this.options = {
onChange,
onEnter,
onExit,
}
this._init();
}
_funcNames = [
"requestFullscreen",
"webkitRequestFullscreen",
"mozRequestFullScreen",
"msRequestFullscreen",
];
_exitFuncNames = [
"exitFullscreen",
"webkitExitFullscreen",
"mozCancelFullScreen",
"msExitFullscreen",
];
_fullScreenNames = [
"fullscreenElement",
"webkitFullscreenElement",
"mozFullScreenElement",
"msFullscreenElement",
];
_init() {
this._enterProptypeName = this._getProptypeName(
this._funcNames,
document.documentElement,
);
this._exitProptypeName = this._getProptypeName(this._exitFuncNames);
}
_getProptypeName(names, target = document) {
return names.find((name) => name in target);
}
fullEle() {
const name = this._getProptypeName(this._fullScreenNames);
return document[name];
}
isFull() {
return !!this.fullEle();
}
toogle() {
this.isFull() ? this.exit() : this.enter();
}
enter() {
this._enterProptypeName && this.el[this._enterProptypeName]();
this.options?.onEnter && this.options.onEnter();
this.options?.onChange && this.options.onChange();
}
exit() {
this._exitProptypeName && document[this._exitProptypeName]();
this.options?.onEnter && this.options.onExit();
this.options?.onChange && this.options.onChange();
}
}
export default FullScreen;