|
| 1 | +const player = document.querySelector('.player'); |
| 2 | +const video = player.querySelector('.viewer'); |
| 3 | +const progress = player.querySelector('.progress'); |
| 4 | +const progressBar = player.querySelector('.progress__filled'); |
| 5 | +const toggle = player.querySelector('.toggle'); |
| 6 | +const skipButtons = player.querySelectorAll('[data-skip]'); |
| 7 | +const ranges = player.querySelectorAll('input'); |
| 8 | + |
| 9 | +function play(){ |
| 10 | + video[video.paused ? 'play' : 'pause']() |
| 11 | + |
| 12 | +} |
| 13 | +function toogleButton(){ |
| 14 | + toggle.textContent = video.paused ? '▶' : '⏸'; |
| 15 | +} |
| 16 | +function skip(){ |
| 17 | + video.currentTime += parseFloat(this.dataset.skip); |
| 18 | + |
| 19 | + |
| 20 | +} |
| 21 | +function updateRange(){ |
| 22 | + video[this.name] = this.value; |
| 23 | + |
| 24 | +} |
| 25 | +function updateProgress(){ |
| 26 | + progressBar.style.flexBasis = `${(video.currentTime / video.duration) * 100}%`; |
| 27 | +} |
| 28 | +function scrub(e){ |
| 29 | + video.currentTime = (e.offsetX / progress.offsetWidth) * video.duration; |
| 30 | +} |
| 31 | +let isMouseDown = false; |
| 32 | +video.addEventListener('click',play); |
| 33 | +video.addEventListener('play', toogleButton); |
| 34 | +video.addEventListener('pause', toogleButton); |
| 35 | +video.addEventListener('timeupdate', updateProgress); |
| 36 | + |
| 37 | + |
| 38 | +toggle.addEventListener('click',play); |
| 39 | +skipButtons.forEach(button => button.addEventListener('click', skip)) |
| 40 | +ranges.forEach(range => range.addEventListener('change',updateRange)) |
| 41 | +ranges.forEach(range => range.addEventListener('mousemove',updateRange)) |
| 42 | +progress.addEventListener('click', scrub); |
| 43 | +progress.addEventListener('mousemove', (e) => isMouseDown && scrub(e)); |
| 44 | +progress.addEventListener('mousedown',() => isMouseDown = true); |
| 45 | +progress.addEventListener('mouseup',() => isMouseDown = false); |
| 46 | +window.addEventListener('keydown', (e) => {if(e.key === ' ') play()}) |
| 47 | + |
0 commit comments