|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// grab the elements |
| 4 | +const player = document.querySelector('.player'); |
| 5 | +const video = player.querySelector('video'); |
| 6 | +const progress = player.querySelector('.progress'); |
| 7 | +const progressBar = player.querySelector('.progress__filled'); |
| 8 | +const toggle = player.querySelector('.toggle'); |
| 9 | +const ranges = player.querySelectorAll('[type="range"]'); |
| 10 | +const skipButtons = player.querySelectorAll('[data-skip]'); |
| 11 | + |
| 12 | + |
| 13 | +// functions |
| 14 | +function togglePlay() { |
| 15 | + if (video.paused) { |
| 16 | + video.play(); |
| 17 | + } else { |
| 18 | + video.pause(); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +function togglePlayIfSpace(event) { |
| 23 | + if (event.keyCode === 32) { |
| 24 | + togglePlay(); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function updateButton() { |
| 29 | + const icon = this.paused ? '►' : '❚❚'; |
| 30 | + toggle.textContent = icon; |
| 31 | +} |
| 32 | + |
| 33 | +function skip() { |
| 34 | + const time = Number.parseFloat(this.dataset.skip); |
| 35 | + video.currentTime += time; |
| 36 | +} |
| 37 | + |
| 38 | +function handleRangeUpdate() { |
| 39 | + video[this.name] = this.value; |
| 40 | +} |
| 41 | + |
| 42 | +function handleProgress() { |
| 43 | + const percentage = video.currentTime / video.duration * 100; |
| 44 | + progressBar.style.flexBasis = percentage + '%'; |
| 45 | +} |
| 46 | + |
| 47 | +function scrub(event) { |
| 48 | + const scrubTime = event.offsetX / progress.offsetWidth * video.duration; |
| 49 | + video.currentTime = scrubTime; |
| 50 | +} |
| 51 | + |
| 52 | +// hook up the event listeners |
| 53 | +video.addEventListener('click', togglePlay); |
| 54 | +toggle.addEventListener('click', togglePlay); |
| 55 | +document.addEventListener('keydown', togglePlayIfSpace); |
| 56 | + |
| 57 | +video.addEventListener('play', updateButton); |
| 58 | +video.addEventListener('pause', updateButton); |
| 59 | +video.addEventListener('timeupdate', handleProgress); |
| 60 | + |
| 61 | +skipButtons.forEach(button => button.addEventListener('click', skip)); |
| 62 | +ranges.forEach(range => range.addEventListener('change', handleRangeUpdate)); |
| 63 | +ranges.forEach(range => range.addEventListener('mousemove', handleRangeUpdate)); |
| 64 | + |
| 65 | +let mouseDown = false; |
| 66 | +progress.addEventListener('click', scrub); |
| 67 | +progress.addEventListener('mousemove', (event) => mouseDown && scrub(event)); |
| 68 | +progress.addEventListener('mousedown', () => {mouseDown = true;}); |
| 69 | +progress.addEventListener('mouseup', () => mouseDown = false); |
| 70 | + |
| 71 | + |
| 72 | +const fullScreenButton = document.querySelector('button#fullscreen'); |
| 73 | + |
| 74 | +function toggleFullScreen() { |
| 75 | + console.log(window.innerWidth, window.innerHeight); |
| 76 | + video.style.width = `${window.innerWidth}px`; |
| 77 | + video.style.height = `${window.innerHeight}px`; |
| 78 | +} |
| 79 | + |
| 80 | +fullScreenButton.addEventListener('click', toggleFullScreen); |
0 commit comments