|
| 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('.player__slider'); |
| 8 | +const fullScreen = player.querySelectorAll('button.size__button'); |
| 9 | + |
| 10 | +// build out functions |
| 11 | +// |
| 12 | +function togglePlay() { |
| 13 | + const playMethod = video.paused ? 'play' : 'pause'; |
| 14 | + video[playMethod](); |
| 15 | +}; |
| 16 | + |
| 17 | +function updateButton() { |
| 18 | + const icon = this.paused ? '►' : '❚ ❚'; |
| 19 | + |
| 20 | + toggle.textContent = icon; |
| 21 | +}; |
| 22 | + |
| 23 | +function skip() { |
| 24 | + const time = this.dataset.skip; |
| 25 | + |
| 26 | + video.currentTime += parseFloat(time) |
| 27 | +}; |
| 28 | + |
| 29 | +function handleRangeUpdate() { |
| 30 | + video[this.name] = this.value; |
| 31 | +}; |
| 32 | + |
| 33 | +function handleProgress() { |
| 34 | + const percent = (video.currentTime / video.duration) * 100; |
| 35 | + |
| 36 | + progressBar.style.flexBasis = `${percent}%`; |
| 37 | +}; |
| 38 | + |
| 39 | +function scrub(e) { |
| 40 | + const scrubTime = (e.offsetX / progress.offsetWidth) * video.duration; |
| 41 | + |
| 42 | + video.currentTime = scrubTime; |
| 43 | +}; |
| 44 | + |
| 45 | +let mousedown = false |
| 46 | + |
| 47 | +// hook up event listeners |
| 48 | +progress.addEventListener('click', scrub); |
| 49 | +progress.addEventListener('mousemove', (e) => mousedown && scrub(e)); |
| 50 | +progress.addEventListener('mousedown', () => mousedown = true); |
| 51 | +progress.addEventListener('mouseup', () => mousedown = false); |
| 52 | + |
| 53 | +video.addEventListener('click', togglePlay); |
| 54 | +video.addEventListener('play', updateButton); |
| 55 | +video.addEventListener('pause', updateButton); |
| 56 | +video.addEventListener('timeupdate', handleProgress); |
| 57 | + |
| 58 | +toggle.addEventListener('click', togglePlay); |
| 59 | +fullScreen[0].addEventListener('click', () => video.webkitEnterFullscreen()) |
| 60 | + |
| 61 | +skipButtons.forEach(button => button.addEventListener('click', skip)) |
| 62 | + |
| 63 | +ranges.forEach(range => range.addEventListener('change', handleRangeUpdate)); |
| 64 | + |
0 commit comments