Flat Responsive HTML5 Video Player With jQuery And CSS3

Flat Responsive HTML5 Video Player With jQuery And CSS3
File Size: 9.98 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

This is a pretty nice, flat style, fully responsive, mobile-friendly HTML5 video player built using jQuery, HTML5, CSS/CSS3 and a little JavaScript.

How to use it:

1. Build the html structure for the video player and embed an HTML5 video into it.

<div id="videoContainer">

   <div class="overlay">
      <div class="button"></div>
   </div>

   <div id="video">
      <video src="sample.mp4">
      </video>
   </div>

   <div id="controls">
      <div class="playButton">
         <div class="playPause"></div>
      </div>

      <div class="ProgressContainer">
         <div class="timer intialTime">
            00:00
         </div>
         <div class="progressBar">
            <div class="progress"></div>
         </div>
         <div class="time">
            00:00
         </div>
         <div class="timer fullTime">
            00:00
         </div>
      </div>

      <div class="volume">
         <div class="icon"></div>

         <div class="intensityBar">
            <div class="intensity"></div>
         </div>
      </div>

      <div class="scale">
         <div class="icon"></div>
      </div>
   </div>

</div>

2. Place the following CSS snippets into your existing CSS file.

#videoContainer {
  width: 70vw;
  height: 65vh;
  background: #39a072;
  margin: 30px auto;
  border-radius: 5px;
  box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.2);
  margin-bottom: 20px;
  position: relative;
  overflow: hidden;
  -webkit-user-select: none;
  -moz-user-select: none;
  user-select: none;
}

#videoContainer.fullScreen {
  width: 100vw !important;
  height: 100vh !important;
}

#videoContainer.small .intensityBar { width: 50px !important; }

#videoContainer.small .playButton {
  margin: 0 !important;
  margin-right: 5px !important;
}

#videoContainer.small .timer { display: none !important; }

#videoContainer.small .playPause, #videoContainer.small .volume .icon, #videoContainer.small .scale .icon {
  width: 15px !important;
  height: 15px !important;
}

#videoContainer.small .progressBar { height: 6px !important; }

#videoContainer.small .overlay .button {
  width: 50px !important;
  height: 50px !important;
}

#videoContainer.small .time { display: none !important; }

#videoContainer .overlay {
  width: 100%;
  height: 100%;
  background: radial-gradient(circle, rgba(77, 191, 140, 0.9), rgba(41, 115, 82, 0.9));
  position: absolute;
  top: 0;
  left: 0;
  z-index: 999;
  border-radius: 5px;
}

#videoContainer .overlay .button {
  width: 80px;
  height: 80px;
  position: absolute;
  top: 50%;
  left: 50%;
  background: url(play-button.svg);
  background-size: 100% 100%;
  transform: translate(-50%, calc(-50% - 30px));
  cursor: pointer;
  transition: width 0.2s, height 0.2s;
}

#videoContainer .overlay .button:hover {
  width: 90px;
  height: 90px;
}

#videoContainer #video {
  width: 100%;
  height: calc(100% - 60px);
  position: relative;
  top: 0;
  left: 0;
  overflow: hidden;
  border-radius: 5px;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

#videoContainer #video video {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 5px;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;
}

#videoContainer #controls {
  width: 100%;
  height: 60px;
  background: #4dbf8c;
  position: absolute;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom-right-radius: 5px;
  border-bottom-left-radius: 5px;
  z-index: 9999;
}

#videoContainer #controls .playButton {
  width: 70px;
  height: 100%;
  background: #4dbf8c;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20px;
  cursor: pointer;
  transition: all 0.4s;
  border-bottom-left-radius: 5px;
}

#videoContainer #controls .playButton:hover { background: #41b682; }

#videoContainer #controls .playButton .playPause {
  width: 25px;
  height: 25px;
  background: url(play.svg);
  background-size: 100% 100%;
}

#videoContainer #controls .ProgressContainer {
  color: #fff;
  width: calc(100% - 100px);
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  position: relative;
}

#videoContainer #controls .ProgressContainer .progressBar {
  width: 100%;
  height: 8px;
  background: #39a072;
  border-radius: 20px;
  cursor: pointer;
  overflow: hidden;
}

#videoContainer #controls .ProgressContainer .progressBar:hover + .time {
  opacity: 1;
  transform: translateY(0);
}

#videoContainer #controls .ProgressContainer .progressBar .progress {
  width: 0%;
  height: 100%;
  background: #fff;
  border-radius: 20px;
}

#videoContainer #controls .ProgressContainer .timer {
  margin: 0 10px;
  font-family: Arial, sans-serif;
  font-weight: 300;
  font-size: 12px;
  font-weight: 300;
  color: #2c7a57;
  letter-spacing: 1px;
}

#videoContainer #controls .ProgressContainer .time {
  width: 80px;
  height: 25px;
  background: #2c7a57;
  position: absolute;
  top: -20px;
  left: 0;
  border-radius: 5px;
  color: #fff;
  font-family: Arial, sans-serif;
  text-align: center;
  line-height: 25px;
  font-size: 12px;
  letter-spacing: 1px;
  opacity: 0;
  transform: translateY(10px);
  transition: transform 0.3s, opacity 0.3s;
}

#videoContainer #controls .ProgressContainer .time::after {
  content: "";
  display: block;
  width: 0;
  height: 0;
  position: absolute;
  top: 25px;
  left: 33px;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid #2c7a57;
}

#videoContainer #controls .volume {
  width: 150px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  position: relative;
}

#videoContainer #controls .volume .icon {
  width: 25px;
  height: 25px;
  background: url(volume.svg);
  background-size: 100% 100%;
  cursor: pointer;
  margin-right: 10px;
}

#videoContainer #controls .volume .intensityBar {
  width: 100px;
  height: 4px;
  background: #39a072;
  border-radius: 20px;
  max-width: 100px;
  overflow: hidden;
  transform-origin: right center;
  cursor: pointer;
  transition: all 0.5s;
}

#videoContainer #controls .volume .intensityBar .intensity {
  width: 50%;
  height: 100%;
  background: #fff;
}

#videoContainer #controls .scale {
  width: 70px;
  height: 100%;
  background: #4dbf8c;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 5px;
  cursor: pointer;
  transition: all 0.4s;
  border-bottom-right-radius: 5px;
}

#videoContainer #controls .scale:hover { background: #40b27f; }

#videoContainer #controls .scale .icon {
  width: 25px;
  height: 25px;
  background-size: 100% 100%;
  background: url(expand.svg);
  background-size: 100% 100%;
}

3. Load the latest version of jQuery JavaScript library (slim build) at the end of the document.

<script src="//code.jquery.com/jquery-3.1.1.slim.min.js"></script> 

4. The main JavaScript snippets to active the video player.

$(document).ready(function () {

    'use strict';

    var play_pause     = $('.playButton'),
        container      = $('#videoContainer'),
        playIcon       = $('.playButton .playPause'),
        video          = $('video'),
        play           = $('.play'),
        volume         = $('.volume .icon'),
        volumeIntesity = $('.volume .intensityBar'),
        intensity      = $('.intensity'),
        progressBar    = $('.progressBar'),
        progress       = $('.progressBar .progress'),
        timer          = $('.intialTime'),
        vidDuration    = $('.fullTime'),
        expandButton   = $('.scale .icon'),
        overlayScreen  = $('.overlay'),
        timeState      = $('.time'),
        overlayButton  = $('.overlay .button'),
        update;


    $(window).resize(function () { resizeVid(); });

    resizeVid();

    updateplayer();

    play_pause.add(video).click(function () { playVid(); });

    progressBar.click(function () {skip(); });

    progressBar.mousemove(function () { timeState.text(getTimeState()); });

    volume.click(function () { toggleMute(); });

    volumeIntesity.click(function () { changeVolume(); });

    expandButton.click(function () { fullScreen(); });

    overlayButton.click(function () { playVid();});
  
    vidDuration.text(getFormatedFullTime());


    function playVid() {
        if (video.get(0).paused) {
            video.get(0).play();
            playIcon.css({
               'background': 'url(pause.svg)',
               'background-size': '100% 100%'
            });
            overlayScreen.hide();
            update = setInterval(updateplayer, 1);

        } else {
            video.get(0).pause();
            playIcon.css({
               'background': 'url(play.svg)',
               'background-size': '100% 100%'
            });
            overlayScreen.show();
            clearInterval(update);
        }
    }
  
    function updateplayer() {
        var percentage = (video[0].currentTime / video[0].duration) * 100;
        progress.css({'width': percentage + '%'});
        timer.text(getFormatedTime());
        vidDuration.text(getFormatedFullTime());

        if (video[0].ended) {
            playIcon.css({
               'background': 'url(play.svg)',
               'background-size': '100% 100%'
            });
            overlayScreen.show();
            overlayButton.css({
               'background': 'url(replay.svg)',
               'background-size': '100% 100%'
            });
        } else if (video[0].paused) {
            overlayButton.css({
               'background': 'url(play-button.svg)',
               'background-size': '100% 100%'
            });
        }
    }

    function getTimeState() {

      var mouseX = event.pageX - progressBar.offset().left,
          width  = progressBar.outerWidth();

       var currentSeconeds = Math.round((mouseX / width) * video[0].duration);
       var currentMinutes  = Math.floor(currentSeconeds/60);

       if (currentMinutes > 0) {
          currentSeconeds -= currentMinutes * 60;
       }
       if (currentSeconeds.toString().length === 1) {
           currentSeconeds = "0" + currentSeconeds;
        }
        if (currentMinutes.toString().length === 1) {
            currentMinutes = "0" + currentMinutes;
        }

        timeState.css('left', (mouseX / width) * progressBar.width() + 18 + 'px');

        return currentMinutes + ':' + currentSeconeds;

    }

    function skip() {
        var mouseX = event.pageX - progressBar.offset().left,
            width  = progressBar.outerWidth();
       video[0].currentTime = (mouseX / width) * video[0].duration;
       updateplayer();
    }

    function getFormatedFullTime() {

         var totalSeconeds = Math.round(video[0].duration);
         var totalMinutes  = Math.floor(totalSeconeds/60);
         if (totalMinutes > 0) {
            totalSeconeds -= totalMinutes * 60;
         }
         if (totalSeconeds.toString().length === 1) {
             totalSeconeds = "0" + totalSeconeds;
          }
          if (totalMinutes.toString().length === 1) {
              totalMinutes = "0" + totalMinutes;
          }
        return totalMinutes + ':' + totalSeconeds;
    }

     function getFormatedTime() {
         var seconeds = Math.round(video[0].currentTime);
         var minutes  = Math.floor(seconeds / 60);

          if (minutes > 0) {
            seconeds -= minutes * 60;
         }
         if (seconeds.toString().length === 1) {
             seconeds = "0" + seconeds;
          }
          if (minutes.toString().length === 1) {
              minutes = "0" + minutes;
          }
         return minutes + ':' + seconeds;
     }

    function toggleMute() {
        if (!video[0].muted) {
           video[0].muted = true;
           volume.css({
              'background': 'url(mute.svg)',
              'background-size': '100% 100%'
           });
           intensity.hide();
        } else {
           video[0].muted = false;
           volume.css({
              'background': 'url(volume.svg)',
              'background-size': '100% 100%'
           });
           intensity.show();
        }
    }

    function changeVolume() {
       var mouseX = event.pageX - volumeIntesity.offset().left,
           width  = volumeIntesity.outerWidth();

       video[0].volume = (mouseX / width);
       intensity.css('width', (mouseX / width) * width + 'px');
       video[0].muted = false;
       volume.css({
          'background': 'url(volume.svg)',
          'background-size': '100% 100%'
       });
       intensity.show();
    }

    function fullScreen() {
        if (video[0].requestFullscreen) {
           video[0].requestFullscreen();
        } else if (video[0].webkitRequestFullscreen) {
           video[0].webkitRequestFullscreen();
        } else if (video[0].mozRequestFullscreen) {
           video[0].mozRequestFullscreen();
        } else if (video[0].msRequestFullscreen) {
           video[0].msRequestFullscreen();
        } else {
           video.dblclick(function () { fullScreen(); });
        }

    }

    function resizeVid() {
       if (container.width() < 600) {
          container.addClass('small');
       } else {
          container.removeClass('small');
       }
    }

    $(window).keypress(function (e) {
       if (e.keyCode === 0 || e.keyCode === 32) {
          e.preventDefault()
          playVid();
       }
    });
  
    for (var i = 0, l = videos.length; i < l; i++) {
    var video = videos[i];
    var src = video.src || (function () {
        var sources = video.querySelectorAll("source");
        for (var j = 0, sl = sources.length; j < sl; j++) {
            var source = sources[j];
            var type = source.type;
            var isMp4 = type.indexOf("mp4") != -1;
            if (isMp4) return source.src;
        }
        return null;
    })();
    if (src) {
        var isYoutube = src && src.match(/(?:youtu|youtube)(?:\.com|\.be)\/([\w\W]+)/i);
        if (isYoutube) {
            var id = isYoutube[1].match(/watch\?v=|[\w\W]+/gi);
            id = (id.length > 1) ? id.splice(1) : id;
            id = id.toString();
            var mp4url = "http://www.youtubeinmp4.com/redirect.php?video=";
            video.src = mp4url + id;
        }
    }
}


});

This awesome jQuery plugin is developed by Mhmdhasan. For more Advanced Usages, please check the demo page or visit the official website.