|
| 1 | +const video = document.querySelector('.player__video'); |
| 2 | +const range = document.querySelectorAll('input[type="range"]'); |
| 3 | +const player = document.querySelector('.viewer'); |
| 4 | +const toggle = document.querySelector('.toggle'); |
| 5 | +const progressBar = document.querySelector('.progress'); |
| 6 | +const progress = document.querySelector('.progress__filled'); |
| 7 | +const playerButton = document.querySelector('.toggle'); |
| 8 | +const skipButtons = document.querySelectorAll('button[data-skip]'); |
| 9 | + |
| 10 | +function scrub(e){ |
| 11 | + const percentage = e.offsetX / video.videoWidth |
| 12 | + video.currentTime = video.duration * percentage |
| 13 | +} |
| 14 | + |
| 15 | +function playVideo(){ |
| 16 | + video.paused ? video.play() : video.pause(); |
| 17 | +} |
| 18 | + |
| 19 | +function skip(){ |
| 20 | + video.currentTime = video.currentTime + Number(this.dataset.skip); |
| 21 | +} |
| 22 | + |
| 23 | +function configChange(){ |
| 24 | + if(this.name == 'volume'){ |
| 25 | + video.volume = this.value; |
| 26 | + }else{ |
| 27 | + video.playbackRate = this.value |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function videoPlaying(){ |
| 32 | + progress.style.flexBasis = (video.currentTime / video.duration) * 100 + "%"; |
| 33 | +} |
| 34 | + |
| 35 | +function updateButton(){ |
| 36 | + toggle.innerHTML = video.paused ? '►' : '||'; |
| 37 | +} |
| 38 | + |
| 39 | +player.addEventListener('click', playVideo) |
| 40 | +video.addEventListener('timeupdate', videoPlaying) |
| 41 | +video.addEventListener('pause', updateButton) |
| 42 | +video.addEventListener('play', updateButton) |
| 43 | +playerButton.addEventListener('click', playVideo) |
| 44 | +progressBar.addEventListener('mousedown', scrub) |
| 45 | +skipButtons.forEach(button => { |
| 46 | + button.addEventListener('click', skip) |
| 47 | +}) |
| 48 | +range.forEach(item => { |
| 49 | + item.addEventListener('mousedown', configChange) |
| 50 | + item.addEventListener('mousemove', configChange) |
| 51 | +}) |
0 commit comments