-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
158 lines (128 loc) · 4.73 KB
/
script.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const player = document.querySelector('.player');
const video = document.querySelector('video');
const progressRange = document.querySelector('.progress-range');
const progressBar = document.querySelector('.progress-bar');
const playPauseButton = document.getElementById('play-pause-button');
const volumeIcon = document.getElementById('volume-icon');
const volumeRange = document.querySelector('.volume-range');
const volumeBar = document.querySelector('.volume-bar');
const speed = document.querySelector('select');
const currentTime = document.querySelector('.time-elapsed');
const duration = document.querySelector('.time-duration');
const pictureInPictureButton = document.querySelector('#picture-in-picture');
const fullScreenButton = document.querySelector('#fullscreen');
function showPlayIcon() {
playPauseButton.classList.replace('fa-pause', 'fa-play');
playPauseButton.setAttribute('title', 'play');
};
function togglePlay() {
if (video.paused) {
video.play();
playPauseButton.classList.replace('fa-play', 'fa-pause');
playPauseButton.setAttribute('title', 'pause');
} else {
video.pause();
showPlayIcon();
};
};
function disPlayTime(time) {
const minutes = Math.floor(time / 60);
let seconds = Math.floor(time % 60);
seconds = seconds > 9 ? seconds : `0${seconds}`;
return `${minutes}:${seconds}`;
};
function upDateProgress() {
progressBar.style.width = `${(video.currentTime / video.duration) * 100}%`;
// displayTime(64);
currentTime.textContent = disPlayTime(video.currentTime);
duration.textContent = disPlayTime(video.duration);
};
function setProgress(event) {
const newTime = event.offsetX / progressRange.offsetWidth;
progressBar.style.width = `${newTime * 100}%`;
video.currentTime = newTime * video.duration;
};
let lastVolume = 1;
function changeVolume(event) {
let volume = event.offsetX / volumeRange.offsetWidth;
if (volume < 0.1) {
volume = 0;
} else if (volume > 0.9) {
volume = 1;
};
volumeBar.style.width = `${volume * 100}%`;
video.volume = volume;
lastVolume = volume;
volumeIcon.className = '';
if (volume > 0.7) {
volumeIcon.classList.add('fas', 'fa-volume-up');
} else if (volume < 0.7 && volume > 0) {
volumeIcon.classList.add('fas', 'fa-volume-down');
} else {
volumeIcon.classList.add('fas', 'fa-volume-off');
};
};
function toggleMute() {
volumeIcon.className = '';
if (video.volume) {
lastVolume = video.volume;
video.volume = 0;
volumeBar.style.width = 0;
volumeIcon.classList.add('fas', 'fa-volume-mute');
volumeIcon.setAttribute('title', 'unMute');
} else {
video.volume = lastVolume;
volumeBar.style.width = `${lastVolume * 100}%`;
if (lastVolume > 0.7) {
volumeIcon.classList.add('fas', 'fa-volume-up');
} else if (lastVolume < 0.7 && lastVolume > 0) {
volumeIcon.classList.add('fas', 'fa-volume-down');
} else {
volumeIcon.classList.add('fas', 'fa-volume-off');
};
volumeIcon.setAttribute('title', 'mute');
};
};
function changeSpeed() {
video.playbackRate = speed.value;
};
function openFullScreen(element) {
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.mozRequestFullScreen) { // FireFox
element.mozRequestFullScreen();
} else if (element.webkitRequestFullscreen) { // Chrome/Safari/Opera
element.webkitRequestFullscreen();
} else if (element.msRequestFullscreen) { // Internet Explorer / Edge
element.msRequestFullscreen();
};
video.classList.add('video-fullscreen');
};
function closeFullScreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) { // Firefox
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) { // Chrome/Safari/Opera
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) { // Internet Explorer / Edge
document.msExitFullscreen();
};
video.classList.remove('video-fullscreen');
};
let fullScreen = false;
function toggleFullScreen() {
!fullScreen ? openFullScreen(player) : closeFullScreen();
fullScreen = !fullScreen;
};
playPauseButton.addEventListener('click', togglePlay);
video.addEventListener('click', togglePlay);
video.addEventListener('canplay', upDateProgress);
video.addEventListener('timeupdate', upDateProgress);
video.addEventListener('ended', showPlayIcon);
progressRange.addEventListener('click', setProgress);
volumeRange.addEventListener('click', changeVolume);
volumeIcon.addEventListener('click', toggleMute);
speed.addEventListener('change', changeSpeed);
pictureInPictureButton.addEventListener('click', async () => {await video.requestPictureInPicture()});
fullScreenButton.addEventListener('click', toggleFullScreen);