|
| 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 | + |
| 6 | +const toggle = player.querySelector('.toggle'); |
| 7 | +const skipButtons = player.querySelectorAll('[data-skip]'); |
| 8 | +const ranges = player.querySelectorAll('.player__slider'); |
| 9 | + |
| 10 | +//build out functions |
| 11 | +function togglePlay(){ |
| 12 | + // if(video.paused){ |
| 13 | + // video.play(); |
| 14 | + // } else { |
| 15 | + // video.pause(); |
| 16 | + // } |
| 17 | + // or use ternary operator |
| 18 | + const method = video.paused ? 'play' : 'pause'; |
| 19 | + video[method](); |
| 20 | +} |
| 21 | +//.paused is a property that lives on the video, no playing property |
| 22 | + |
| 23 | +function updateButton(){ |
| 24 | + const icon = this.paused ? '>' : '||'; |
| 25 | + //the this is bound to the video |
| 26 | + toggle.textContent = icon; |
| 27 | + console.log('Update the button'); |
| 28 | + // |
| 29 | +} |
| 30 | + |
| 31 | +function skip(){ |
| 32 | + console.log(this.dataset.skip); |
| 33 | + //object with value of -10 or 25 for the two buttons |
| 34 | + video.currentTime += parseFloat(this.dataset.skip); |
| 35 | + //parseFloat converts into true number |
| 36 | +} |
| 37 | + |
| 38 | +function handleRangeUpdate(){ |
| 39 | + video[this.name] = this.value; |
| 40 | + // video['volume'] |
| 41 | + // video['playbackRate'] |
| 42 | + //volume and playbackRate are the two properties you want to update |
| 43 | + console.log(this.name); |
| 44 | + console.log(this.value); |
| 45 | +} |
| 46 | + |
| 47 | +function handleProgress(){ |
| 48 | + const percent = (video.currentTime / video.duration) * 100; |
| 49 | + //currentTime and duration are properties on the video |
| 50 | + progressBar.style.flexBasis = `${percent}%`; |
| 51 | +} |
| 52 | + |
| 53 | +function scrub(e){ |
| 54 | + const scrubTime = (e.offsetX / progress.offsetWidth) * video.duration; |
| 55 | + //offsetWidth will give you entire width of bar |
| 56 | + //then update |
| 57 | + video.currentTime = scrubTime; |
| 58 | + console.log(e) |
| 59 | +} |
| 60 | + |
| 61 | +video.addEventListener('click', togglePlay); |
| 62 | + |
| 63 | +video.addEventListener('play', updateButton); |
| 64 | +video.addEventListener('pause', updateButton); |
| 65 | + |
| 66 | +video.addEventListener('timeupdate', handleProgress); |
| 67 | + |
| 68 | +toggle.addEventListener('click', togglePlay); |
| 69 | +skipButtons.forEach(button => button.addEventListener('click', skip)); |
| 70 | +ranges.forEach(range => range.addEventListener('change', handleRangeUpdate)); |
| 71 | +ranges.forEach(range => range.addEventListener('mousemove', handleRangeUpdate)); |
| 72 | + |
| 73 | +let mousedown = false; |
| 74 | +//flag variable |
| 75 | +progress.addEventListener('click', scrub); |
| 76 | +progress.addEventListener('mousemove', (e) => mousedown && scrub(e)); |
| 77 | +//will first check the variable mousedown, if true it moves onto function |
| 78 | + |
| 79 | +progress.addEventListener('mousedown', () => mousedown=true); |
| 80 | +progress.addEventListener('mouseup', () => mousedown=false); |
0 commit comments