-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathindex.js
93 lines (75 loc) · 2.19 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const player = document.querySelector('.player')
const video = player.querySelector('video');
const progress = player.querySelector('.progress');
const progressBar = player.querySelector('.progress_filled');
const toggle = player.querySelector('.toggle');
const skipButtons = player.querySelectorAll('[data-skip]');
const ranges = player.querySelectorAll('.player_slider');
// functions
function togglePlay()
{
if(video.paused)
{
video.play();
}
else
{
video.pause();
}
}
function updateButton(){
// const icon = this.paused ? '►' : '❚ ❚';
// toggle.textContent = icon;
const icon = this.paused ? '►' : '<b>||</b>';
toggle.innerHTML = icon;
}
function skip(){
video.currentTime += parseFloat(this.dataset.skip);
}
function handleRangeUpdate(){
video[this.name] = this.value;
console.log(this);
}
function handleProgress(){
const percent = ( video.currentTime / video.duration ) * 100;
progressBar.style.flexBasis = `${percent}%`;
}
function scrub(e){
let scrubTime = ( e.offsetX / progress.offsetWidth ) * video.duration ;
video.currentTime = scrubTime;
console.log(e);
}
// eventListeners
// 1.play/pause button
video.addEventListener('click', togglePlay);
video.addEventListener('play', updateButton);
video.addEventListener('pause', updateButton);
window.addEventListener('keydown', (e) => {
if(e.key === " ")
{
togglePlay();
updateButton();
}
});
toggle.addEventListener('click', togglePlay);
// skip buttons
skipButtons.forEach(button => button.addEventListener('click', skip));
window.addEventListener('keydown',(e) => {
if(e.key === "ArrowLeft")
{
skipButtons[0].click();
}
if(e.key === "ArrowRight")
{
skipButtons[1].click();
}
})
// range inputs
ranges.forEach(range => range.addEventListener('change', handleRangeUpdate))
// progressBar
video.addEventListener('timeupdate', handleProgress)
let mousedown = false;
progress.addEventListener('click', scrub)
progress.addEventListener('mousemove', (e) => mousedown && scrub(e) );
progress.addEventListener('mousedown', () => mousedown = true );
progress.addEventListener('mouseup', () => mousedown = false );