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