<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Video Speed Control</title>

  <link rel="stylesheet" href="./styles.css">
</head>
<body>
  <video src="./scrat.mp4" width="600" height="400" controls></video>

  <div class="speed">
    <div class="speed-bar">1.0</div>
  </div>

  <script src="./scripts.js"></script>
</body>
</html>
@import url('https://fonts.googleapis.com/css?family=Erica+One');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  background: linear-gradient(to top, #cd9cf2 0%, #f6f3ff 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

video {
  height: 100%;
  object-fit: fill;
}

.speed {
  width: 50px;
  height: 350px;
  background: #fff;
  margin-left: 30px;
  border-radius: 20px;
  overflow: hidden;
}

.speed-bar {
  height: 71.45px;
  width: 100%;
  color: #c5c5c5;
  text-align: center;
  user-select: none;
  padding-top: 25px;
  background: linear-gradient(to right, #33001b, #ff0084);
  text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1),
               -1px 1px 1px rgba(0, 0, 0, 0.1);
}

@media screen and (max-width: 780px) {
  video { width: 75vw; }

  .speed {
    width: 20px;
    height: 170px;
    font-size: 12px;
  }
}
const speed = document.querySelector('.speed'),
      bar = document.querySelector('.speed-bar'),
      video = document.querySelector('video'),

      min = 0.5,
      max = 4,
      padding = parseInt(window.getComputedStyle(bar).paddingTop),
      bgHeight = speed.offsetHeight - padding;

let isMouseDown = false;

function setSpeed(e) {
  if (!isMouseDown) return;

  let playbackRate = e.offsetY * (max - min) / bgHeight + min;

  bar.style.height = e.offsetY + 'px';
  bar.textContent = playbackRate.toFixed(1);
  video.playbackRate = playbackRate;
}

bar.addEventListener('mousemove', setSpeed);
bar.addEventListener('mousedown', () =>   isMouseDown = true);
bar.addEventListener('mouseup', () => isMouseDown = false);

speed.addEventListener('mousemove', setSpeed);
speed.addEventListener('mousedown', () =>   isMouseDown = true);
speed.addEventListener('mouseup', () => isMouseDown = false);
speed.addEventListener('mouseleave', () => isMouseDown = false);