-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript2.js
More file actions
78 lines (66 loc) · 2.3 KB
/
script2.js
File metadata and controls
78 lines (66 loc) · 2.3 KB
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
// fixed JS, source: https://github.com/PandAlex/20WebProjectsWithVanillaJavaScript
const video = document.getElementById('video');
const play = document.getElementById('play');
const stop = document.getElementById('stop');
const progress = document.getElementById('progress');
const timestamp = document.getElementById('timestamp');
let shouldUpdateVideo = true;
/**
* Creates a timestamp format from the input in seconds
* @param {Number} time
* @returns {string}
*/
function secondsToTimestamp(time) {
const mins = Math.floor(time / 60);
const seconds = Math.floor(time % 60);
return `${mins < 10 ? '0' : ''}${mins}:${seconds < 10 ? '0' : ''}${seconds.toFixed(0)}`
}
// Play and Pause video
function toggleVideoStatus() {
if(video.paused) {
video.play();
} else {
video.pause();
}
}
// Updates the play/pause icon
function updatePlayIcon() {
if(video.paused) {
play.innerHTML = '<i class="fa fa-play fa-2x"></i>';
} else {
play.innerHTML = '<i class="fa fa-pause fa-2x"></i>';
}
}
// Updates both the progressBar and the timestamp elements
function updateProgress() {
if(!shouldUpdateVideo) {
return;
}
progress.value = (video.currentTime / video.duration) * 100;
timestamp.innerText = secondsToTimestamp(video.currentTime) + '/' + secondsToTimestamp(video.duration);
}
// Set video time to progress
function setVideoProgress() {
shouldUpdateVideo = true;
video.currentTime = (parseInt(progress.value) / 100) * video.duration;
}
// Stop video
function stopVideo() {
video.currentTime = 0;
video.pause();
}
// Event Listeners for the video element
video.addEventListener('click', toggleVideoStatus);
video.addEventListener('pause', updatePlayIcon);
video.addEventListener('play', updatePlayIcon);
video.addEventListener('timeupdate', updateProgress);
// Event Listeners for the play button
play.addEventListener('click', toggleVideoStatus);
// Event Listeners for the stop button
stop.addEventListener('click', stopVideo);
// Event Listeners for the progress bar
progress.addEventListener('change', setVideoProgress);
progress.addEventListener('input', e => {
shouldUpdateVideo = false;
timestamp.innerText = secondsToTimestamp(video.duration * (e.target.value / 100)) + '/' + secondsToTimestamp(video.duration);;
})