HTML5 Video

Basic player with custom controls

by Mukul kant

HTML

<div class="wrap">
  
  <!-- default controls enabled at the bottom -->
  <video autoplay controls loop id="player">
    <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4" />
  </video>
  
  
  <!-- example custom controls, placed on top -->
  <div class="controls">
    <i class="material-icons">pause</i>
    <i class="material-icons">volume_up</i>
  </div>
  
</div>

CSS

html, body {
  height: 100vh
}
body {
  background: #111
}
.wrap {
  position: relative;
  width: 480px;
  height: 270px;
  top: 50%;left:50%;
  transform: translate(-50%,-50%)
}

.wrap video {
  width: 100%;
}

.wrap .controls {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity 1s ease;
}

.wrap .controls i {
  color: white;
  padding: 10px;
  cursor: pointer;
}

.wrap .controls i:last-child {
  float: right;
}

.wrap .controls i:hover {
  background: rgba(0, 0, 0, 0.5);
}

.wrap:hover .controls {
  opacity: 1;
  transition: opacity 0.15s ease;
}


/* include material design icon library instead */

@font-face {
  font-family: 'Material Icons';
  font-style: normal;
  font-weight: 400;
  src: local('Material Icons'), local('MaterialIcons-Regular'), url(https://fonts.gstatic.com/s/materialicons/v18/2fcrYFNaTjcS6g4U3t-Y5ZjZjT5FdEJ140U2DJYC3mY.woff2) format('woff2');
}

.material-icons {
  font-family: 'Material Icons';
  font-weight: normal;
  font-style: normal;
  font-size: 24px;
  line-height: 1;
  letter-spacing: normal;
  text-transform: none;
  display: inline-block;
  white-space: nowrap;
  word-wrap: normal;
  direction: ltr;
  -moz-font-feature-settings: 'liga';
  -moz-osx-font-smoothing: grayscale;
}

JavaScript

(function(){
  var p = document.getElementById('player');
  var c = document.querySelector('.controls');
  var playState = c.children[0];
  var volState = c.children[1];  
  playState.addEventListener('click', function(){
  	this.innerText == 'pause' ?
  	(p.pause(), playState.innerText = "play_arrow"):
  	(p.play(), playState.innerText = "pause");
  }); 
  volState.addEventListener('click', function(){
  	this.innerText == 'volume_up' ?
  	(p.muted=true, volState.innerText = "volume_off"):
  	(p.muted=false, volState.innerText = "volume_up");
  });
})();