Created
July 21, 2020 14:28
-
-
Save VasVV/6e16778745d082375bb6ec4cfa4cfb87 to your computer and use it in GitHub Desktop.
Video Player - Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const player = document.querySelector('.player'); | |
| const viewer = player.querySelector('.viewer'); | |
| const progress = player.querySelector('.progress'); | |
| const progressBar = player.querySelector('.progress__filled'); | |
| const toggle = player.querySelector('.toggle'); | |
| const skipButtons = player.querySelectorAll('[data-skip]'); | |
| const ranges = player.querySelectorAll('.player__slider'); | |
| const fullscreenbtn = player.querySelector('.fullscreen'); | |
| const submitBtn = player.querySelector('.submit'); | |
| fullscreenbtn.addEventListener('click', () => { | |
| return player.requestFullscreen() | |
| }) | |
| const togglePlay = () => viewer.paused ? viewer.play() : viewer.pause(); | |
| const playPause = () => viewer.paused ? toggle.textContent = '!!' : toggle.textContent = '►'; | |
| viewer.addEventListener('click', togglePlay) | |
| toggle.addEventListener('click', togglePlay) | |
| viewer.addEventListener('play', playPause); | |
| viewer.addEventListener('pause', playPause); | |
| skipButtons.forEach(e =>{ | |
| e.addEventListener('click', () => { | |
| viewer.currentTime+=Number(e.dataset.skip); | |
| })}); | |
| ranges.forEach(e => { | |
| e.addEventListener('change', () => { | |
| viewer[e.name] = e.value; | |
| }) | |
| }) | |
| setInterval(() => { | |
| let x = (viewer.currentTime / viewer.duration) * 100; | |
| // console.log(x); | |
| // console.log(progressBar.style); | |
| progressBar.style.flexBasis = `${x}%` | |
| }, 1000) | |
| progress.addEventListener('click', (click) => { | |
| console.log(click); | |
| let x = (click.offsetX / progress.offsetWidth) * viewer.duration; | |
| viewer.currentTime = x; | |
| console.log(x); | |
| }) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| html { | |
| box-sizing: border-box; | |
| } | |
| *, *:before, *:after { | |
| box-sizing: inherit; | |
| } | |
| body { | |
| margin: 0; | |
| padding: 0; | |
| display: flex; | |
| background: #7A419B; | |
| min-height: 100vh; | |
| background: linear-gradient(135deg, #7c1599 0%,#921099 48%,#7e4ae8 100%); | |
| background-size: cover; | |
| align-items: center; | |
| justify-content: center; | |
| } | |
| .player { | |
| max-width: 750px; | |
| border: 5px solid rgba(0,0,0,0.2); | |
| box-shadow: 0 0 20px rgba(0,0,0,0.2); | |
| position: relative; | |
| font-size: 0; | |
| overflow: hidden; | |
| } | |
| /* This css is only applied when fullscreen is active. */ | |
| .player:fullscreen { | |
| max-width: none; | |
| width: 100%; | |
| } | |
| .player:-webkit-full-screen { | |
| max-width: none; | |
| width: 100%; | |
| } | |
| .player__video { | |
| width: 100%; | |
| } | |
| .player__button { | |
| background: none; | |
| border: 0; | |
| line-height: 1; | |
| color: white; | |
| text-align: center; | |
| outline: 0; | |
| padding: 0; | |
| cursor: pointer; | |
| max-width: 50px; | |
| } | |
| .player__button:focus { | |
| border-color: #ffc600; | |
| } | |
| .player__slider { | |
| width: 10px; | |
| height: 30px; | |
| } | |
| .player__controls { | |
| display: flex; | |
| position: absolute; | |
| bottom: 0; | |
| width: 100%; | |
| transform: translateY(100%) translateY(-5px); | |
| transition: all .3s; | |
| flex-wrap: wrap; | |
| background: rgba(0,0,0,0.1); | |
| } | |
| .player:hover .player__controls { | |
| transform: translateY(0); | |
| } | |
| .player:hover .progress { | |
| height: 15px; | |
| } | |
| .player__controls > * { | |
| flex: 1; | |
| } | |
| .progress { | |
| flex: 10; | |
| position: relative; | |
| display: flex; | |
| flex-basis: 100%; | |
| height: 5px; | |
| transition: height 0.3s; | |
| background: rgba(0,0,0,0.5); | |
| cursor: ew-resize; | |
| } | |
| .progress__filled { | |
| width: 50%; | |
| background: #ffc600; | |
| flex: 0; | |
| flex-basis: 50%; | |
| } | |
| /* unholy css to style input type="range" */ | |
| input[type=range] { | |
| -webkit-appearance: none; | |
| background: transparent; | |
| width: 100%; | |
| margin: 0 5px; | |
| } | |
| input[type=range]:focus { | |
| outline: none; | |
| } | |
| input[type=range]::-webkit-slider-runnable-track { | |
| width: 100%; | |
| height: 8.4px; | |
| cursor: pointer; | |
| box-shadow: 1px 1px 1px rgba(0, 0, 0, 0), 0 0 1px rgba(13, 13, 13, 0); | |
| background: rgba(255,255,255,0.8); | |
| border-radius: 1.3px; | |
| border: 0.2px solid rgba(1, 1, 1, 0); | |
| } | |
| input[type=range]::-webkit-slider-thumb { | |
| height: 15px; | |
| width: 15px; | |
| border-radius: 50px; | |
| background: #ffc600; | |
| cursor: pointer; | |
| -webkit-appearance: none; | |
| margin-top: -3.5px; | |
| box-shadow:0 0 2px rgba(0,0,0,0.2); | |
| } | |
| input[type=range]:focus::-webkit-slider-runnable-track { | |
| background: #bada55; | |
| } | |
| input[type=range]::-moz-range-track { | |
| width: 100%; | |
| height: 8.4px; | |
| cursor: pointer; | |
| box-shadow: 1px 1px 1px rgba(0, 0, 0, 0), 0 0 1px rgba(13, 13, 13, 0); | |
| background: #ffffff; | |
| border-radius: 1.3px; | |
| border: 0.2px solid rgba(1, 1, 1, 0); | |
| } | |
| input[type=range]::-moz-range-thumb { | |
| box-shadow: 0 0 0 rgba(0, 0, 0, 0), 0 0 0 rgba(13, 13, 13, 0); | |
| height: 15px; | |
| width: 15px; | |
| border-radius: 50px; | |
| background: #ffc600; | |
| cursor: pointer; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css" rel="stylesheet" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div class="player"> | |
| <video class="player__video viewer" src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"></video> | |
| <div class="player__controls"> | |
| <div class="progress"> | |
| <div class="progress__filled"></div> | |
| </div> | |
| <button class="player__button toggle" title="Toggle Play">►</button> | |
| <input type="range" name="volume" class="player__slider" min="0" max="1" step="0.05" value="1"> | |
| <input type="range" name="playbackRate" class="player__slider" min="0.5" max="2" step="0.1" value="1"> | |
| <button data-skip="-10" class="player__button">« 10s</button> | |
| <button data-skip="10" class="player__button">10s »</button> | |
| <button class="player__button fullscreen">◻</button> | |
| </div> | |
| </div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment