Smartify Your Video Embeds!

This tutorial code shows how to embed YouTube, Vimeo and self-hosted videos in HTML pages, customize them and control them via JavaScript. author(s): Edoardo Nosotti

HTML

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <!-- Font Awesome -->
    <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

    <link rel="stylesheet" type="text/css" href="demo.css">

    <title>Smartify Your Video Embeds - Tutorial</title>
  </head>
  <body>

    <h1 class="container">Video embed demos</h1>

    <div class="container">
      <div class="row">

        <!-- Youtube video, made responsive with CSS styles only -->
        <!-- Check the:
             .responsive-169
             .responsive-169 iframe
             styles in demo.css for information.
             The .do-not-resize class prevents the JS script
             to resize this element. -->
        <div class="col-md-6 col-sm-12">
          <p>Responsive YouTube video (CSS only)</p>
          <div class="responsive-169">
            <iframe
              class="do-not-resize"
              src="https://www.youtube.com/embed/vmR8mg3zPRg"
              allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
              frameborder="0"
              allowfullscreen
              ></iframe>
          </div>
        </div>

        <!-- Vimeo video, made responsive with CSS styles only -->
        <!-- Check the:
             .responsive-169
             .responsive-169 iframe
             styles in demo.css for information.
             The .do-not-resize class prevents the JS script
             to resize this...

CSS

video {
  width: 100%;
}

.responsive-169 {
  padding: 56.25% 0 0 0;
  position: relative;
}

.responsive-169 iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.custom-video-cover {
  display: table-cell;
  background-size: cover;
  color: #fff;
  vertical-align: middle;
  text-align: center;
  text-decoration: none;
}

.custom-video-cover:hover {
  color: rgba(255, 255, 255, 1);
  text-decoration: none;
}

h1 {
  margin: 20px 0;
}

p {
  margin: 0 0 10px 0;
  padding: 0;
}

[class^="col-"] {
  padding: 20px;
  margin-bottom: 20px;
}

JavaScript

function getVideoEmbeds() {
  /* Selectors were taken from FitVids.js and edited:
   * https://github.com/davatron5000/FitVids.js/blob/f72b623e466bfe596b5800458d6043675ffb091b/jquery.fitvids.js
   * It is worth checking that code because it has
   * some possibly handy additional features, not
   * ported here to keep this tutorial simple. */
  var selectors = [
    'iframe[src*="player.vimeo.com"]',
    'iframe[src*="youtube.com"]',
    'iframe[src*="youtube-nocookie.com"]',
    'iframe[src*="kickstarter.com"][src*="video.html"]',
    'video'
  ];

  var videos = $('body').find(selectors.join(','));
  // Again, kudos to the FitVids.js team!
  return videos.not('.do-not-resize');
}

function resizeVideos(elements) {
  $(elements).each(function() {
    var aspectRatio = $(this).data("aspect-ratio");
    aspectRatio = aspectRatio || "16:9";
    var ratioXY = aspectRatio.split(":");
    var width = $(this).width();
    var height = parseInt((width * parseInt(ratioXY[1])) / ratioXY[0]);
    $(this).height(height);
  });
}

function setCustomCovers(elements) {
  $(elements).each(function() {
    var customCover = $(this).data("custom-cover");
    if (customCover) {
      var coverElement = $('<a href="#" class="custom-video-cover"></a>');
      var label = $(this).data("custom-cover-label");
      if (label) {
        coverElement.html(label);
      }
      coverElement.css('background-image', 'url(' + customCover + ')');
      coverElement.width($(this).width());
      coverElement.height($(this).height());
      coverElement.data('video-element', $(this));
      $(this).replaceWith(coverElement);
    }
  });

  setCustomCoverEventListeners();
}

function setCustomCoverEventListeners() {
  $('.custom-video-cover').click(function(e) {
    e.preventDefault();
    $(this).replaceWith($(this).data('video-element'));
  });
}

function setCustomVideoControls() {
  /* Look for a <video> element wrapped into the same
   * container as the elements with the '.btn-play' and
  ...