-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
167 lines (147 loc) · 4.98 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
159
160
161
162
163
164
165
166
167
const player = document.querySelector('.player');
const video = document.querySelector('.video');
const progressRange = document.querySelector('.progress-range');
const progressBar = document.querySelector('.progress-bar');
const playBtn = document.getElementById('play-btn');
const volumeIcon = document.getElementById('volume-icon');
const volumeRange = document.querySelector('.volume-range');
const volumeBar = document.querySelector('.volume-bar');
const speed = document.querySelector('.player-speed');
const currentTime = document.querySelector('.time-elapsed');
const duration = document.querySelector('.time-duration');
const fullscreenBtn = document.querySelector('.fullscreen');
// Play & Pause ----------------------------------- //
function showPlayIcon() {
playBtn.classList.replace('fa-pause', 'fa-play');
playBtn.setAttribute('title', 'Play');
}
function togglePlay() {
if (video.paused) {
video.play();
playBtn.classList.replace('fa-play', 'fa-pause');
playBtn.setAttribute('title', 'Pause');
} else {
video.pause();
showPlayIcon();
}
}
// On video end, show play button icon
video.addEventListener('ended', showPlayIcon);
// Progress Bar ---------------------------------- //
// Format current time, duration
function displayTime(time) {
const minutes = Math.floor(time / 60);
let seconds = Math.floor(time % 60);
seconds = seconds > 9 ? seconds : `0${seconds}`;
return `${minutes}:${seconds}`;
}
// Update progress bar as video plays
function updateProgress() {
progressBar.style.width = `${(video.currentTime / video.duration) * 100}%`;
currentTime.textContent = `${displayTime(video.currentTime)} /`;
duration.textContent = `${displayTime(video.duration)}`;
}
// Click to seek within the video
function setProgress(e) {
const newTime = e.offsetX / progressRange.offsetWidth;
progressBar.style.width = `${newTime * 100}%`;
video.currentTime = newTime * video.duration;
}
// Volume Controls --------------------------- //
let lastVolume = 1;
// Mute
function toggleMute() {
volumeIcon.className = '';
if (video.volume) {
lastVolume = video.volume;
video.volume = 0;
volumeIcon.classList.add('fas', 'fa-volume-mute');
volumeIcon.setAttribute('title', 'Unmute');
volumeBar.style.width = 0;
} else {
video.volume = lastVolume;
volumeIcon.classList.add('fas', 'fa-volume-up');
volumeIcon.setAttribute('title', 'Mute');
volumeBar.style.width = `${lastVolume * 100}%`;
}
}
// Volume Bar
function changeVolume(e) {
let volume = e.offsetX / volumeRange.offsetWidth;
// Rounding volume up or down
if (volume < 0.1) {
volume = 0;
}
if (volume > 0.9) {
volume = 1;
}
volumeBar.style.width = `${volume * 100}%`;
video.volume = volume;
// Change icon depending on 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 if (volume === 0) {
volumeIcon.classList.add('fas', 'fa-volume-off');
}
lastVolume = volume;
}
// Change Playback Speed -------------------- //
function changeSpeed() {
video.playbackRate = speed.value;
}
// Fullscreen ------------------------------- //
/* View in fullscreen */
function openFullscreen(element) {
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.mozRequestFullScreen) {
/* Firefox */
element.mozRequestFullScreen();
} else if (element.webkitRequestFullscreen) {
/* Chrome, Safari and Opera */
element.webkitRequestFullscreen();
} else if (element.msRequestFullscreen) {
/* IE/Edge */
element.msRequestFullscreen();
}
video.classList.add('video-fullscreen');
}
/* Close fullscreen */
function closeFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
/* Firefox */
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
/* Chrome, Safari and Opera */
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
/* IE/Edge */
document.msExitFullscreen();
}
video.classList.remove('video-fullscreen');
}
let fullscreen = false;
// Toggle fullscreen
function toggleFullscreen() {
if (!fullscreen) {
openFullscreen(player);
} else {
closeFullscreen();
}
fullscreen = !fullscreen;
}
// Event Listeners
playBtn.addEventListener('click', togglePlay);
video.addEventListener('click', togglePlay);
video.addEventListener('timeupdate', updateProgress);
video.addEventListener('canplay', updateProgress);
progressRange.addEventListener('click', setProgress);
volumeRange.addEventListener('click', changeVolume);
volumeIcon.addEventListener('click', toggleMute);
speed.addEventListener('change', changeSpeed);
fullscreenBtn.addEventListener('click', toggleFullscreen);