|
| 1 | +// Get our elements on the page |
| 2 | +const player = document.querySelector('.player'); |
| 3 | +const video = player.querySelector('.viewer'); |
| 4 | +const progress = player.querySelector('.progress'); |
| 5 | +const progressBar = player.querySelector('.progress__filled'); |
| 6 | +const toggle = player.querySelector('.toggle'); |
| 7 | +const skipButtons = player.querySelectorAll('[data-skip]'); |
| 8 | +const ranges = player.querySelectorAll('.player__slider'); |
| 9 | +// Build our functions |
| 10 | +function togglePlay() { |
| 11 | + // if (video.paused) { |
| 12 | + // video.play(); |
| 13 | + // } else { |
| 14 | + // video.pause(); |
| 15 | + // } |
| 16 | + const method = video.paused ? 'play' : 'pause'; |
| 17 | + video[method](); |
| 18 | +} |
| 19 | +function updateButton() { |
| 20 | + const icon = this.paused ? '►' : '❚ ❚'; |
| 21 | + toggle.textContent = icon; |
| 22 | +} |
| 23 | +function skip() { |
| 24 | + video.currentTime += parseFloat(this.dataset.skip); |
| 25 | +} |
| 26 | +function handleRangeUpdate() { |
| 27 | + video[this.name] = this.value; |
| 28 | +} |
| 29 | +function handleProgress() { |
| 30 | + const percent = (video.currentTime / video.duration) * 100; |
| 31 | + progressBar.style.flexBasis = `${percent}%`; |
| 32 | +} |
| 33 | +function scrub(e) { |
| 34 | + const scrubTime = (e.offsetX / progress.offsetWidth) * video.duration; |
| 35 | + video.currentTime = scrubTime; |
| 36 | + console.log(e); |
| 37 | +} |
| 38 | +// Hook up the event listeners |
| 39 | +video.addEventListener('click', togglePlay); |
| 40 | +video.addEventListener('play', updateButton); |
| 41 | +video.addEventListener('pause', updateButton); |
| 42 | +video.addEventListener('timeupdate', handleProgress); |
| 43 | + |
| 44 | +toggle.addEventListener('click', togglePlay); |
| 45 | +skipButtons.forEach(button => button.addEventListener('click', skip)); |
| 46 | +ranges.forEach(range => range.addEventListener('change', handleRangeUpdate)); |
| 47 | + |
| 48 | +let mousedown = false; |
| 49 | +progress.addEventListener('click', scrub); |
| 50 | +progress.addEventListener('mousemove', (e) => mousedown && scrub(e)); |
| 51 | +progress.addEventListener('mousedown', () => mousedown = true); |
| 52 | +progress.addEventListener('mouseup', () => mousedown = false); |
0 commit comments