-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
163 lines (144 loc) · 4.8 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
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 currentTime = document.querySelector(".time-elapsed");
const duration = document.querySelector(".time-duration");
const speed = document.querySelector(".player-speed");
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 ---------------------------------- //
// Calculate distlay time format
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 progress = e.offsetX / progressRange.offsetWidth;
progressBar.style.width = `${progress * 100}%`;
video.currentTime = progress * video.duration;
}
// Volume Controls --------------------------- //
let lastVolume = 1;
// Change unmuted volume icon depending on volume
function changeUnmutedVolumeIcon(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");
}
}
// 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
changeUnmutedVolumeIcon(volume);
lastVolume = volume;
}
// Mute/unmute
function toggleMute() {
if (video.volume) {
video.volume = 0;
volumeBar.style.width = 0;
volumeIcon.className = "";
volumeIcon.classList.add("fas", "fa-volume-mute");
volumeIcon.setAttribute("title", "Unmute");
} else {
video.volume = lastVolume;
volumeBar.style.width = `${lastVolume * 100}%`;
changeUnmutedVolumeIcon(lastVolume);
volumeIcon.setAttribute("title", "Mute");
}
}
// Change Playback Speed -------------------- //
function changeSpeed() {
video.playbackRate = speed.value;
}
// Fullscreen ------------------------------- //
/* View in fullscreen */
function openFullscreen(elem) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.webkitRequestFullscreen) {
/* Safari */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) {
/* IE11 */
elem.msRequestFullscreen();
}
video.classList.add("video-fullscreen");
}
/* Close fullscreen */
function closeFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
/* Safari */
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
/* IE11 */
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);