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