Skip to content

Commit 6dec3a6

Browse files
author
mjdude
committed
completed progess bar
1 parent 5369c01 commit 6dec3a6

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

11 - Custom Video Player/scripts.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
// additional task : Try and make the full screen button work
2+
13
// Get elements
24
const player = document.querySelector('.player');
35
const video = player.querySelector('.viewer');
46
const progress = player.querySelector('.progress');
5-
const progerssBar = player.querySelector('.progress__filled');
7+
const progressBar = player.querySelector('.progress__filled');
68
const toggle = player.querySelector('.toggle');
79
const skipButtons = player.querySelectorAll('[data-skip]');
810
const ranges = player.querySelectorAll('.player__slider');
@@ -31,21 +33,35 @@ function skip(){
3133

3234
function handleRangeUpdate(){
3335
console.log(this.name, this.value);
34-
if (this.name === 'volume') {
35-
video.volume = this.value;
36-
};
36+
// you could do it this way
37+
// if (this.name === 'volume') {
38+
// video.volume = this.value;
39+
// };
3740

38-
if (this.name === 'playbackRate') {
39-
video.playbackRate = this.value
40-
};
41+
// if (this.name === 'playbackRate') {
42+
// video.playbackRate = this.value
43+
// };
44+
45+
// Alternativly
46+
video[this.name] = this.value;
47+
}
4148

49+
function handleProgress(){
50+
const percent = (video.currentTime / video.duration) * 100;
51+
progressBar.style.flexBasis = `${percent}%`;
4252
}
4353

54+
function scrub(e){
55+
console.log(e);
56+
const scrubTime = (e.offsetX / progress.offsetWidth) * video.duration;
57+
video.currentTime = scrubTime;
58+
}
4459
// Hook up event listeners
4560

4661
video.addEventListener('click', updateButton);
4762
video.addEventListener('play', updateButton);
4863
video.addEventListener('pause', updateButton);
64+
video.addEventListener('timeupdate', handleProgress);
4965

5066
toggle.addEventListener('click', togglePlay);
5167

@@ -54,5 +70,17 @@ skipButtons.forEach((button) => {
5470
});
5571

5672
ranges.forEach((range) => {
57-
range.addEventListener('click', handleRangeUpdate);
58-
});
73+
range.addEventListener('change', handleRangeUpdate);
74+
});
75+
76+
ranges.forEach((range) => {
77+
range.addEventListener('mousemove', handleRangeUpdate);
78+
});
79+
80+
81+
// check mousedown to make sure we arent always running scrub when we dont need to
82+
let mouseDown = false;
83+
progress.addEventListener('click', scrub);
84+
progress.addEventListener('mousemove', (e) => mouseDown && scrub(e));
85+
progress.addEventListener('mousedown', mouseDown = true );
86+
progress.addEventListener('mouseup', mouseDown = false );

0 commit comments

Comments
 (0)