Micro YouTube Video Lazy Loader
Tips & Tricks

Micro YouTube Video Lazy Loader

When you embed a YouTube video iframe, maybe via their 'Copy Embed Code' feature, it will load a bunch of JavaScript, which will slow down your site on load. This method only displays the video cover image (lazy loaded WEBP), until the user interacts with it.

••52 views

Demo

YouTube Video

Usage

All you need is a DIV with class 'youtube' and the 11 digit YouTube video ID...

<div class="youtube rounded mb-4" data-embed="roLwUDzRaxs">YouTube Video</div>

If you prefer, you can use your own cover image, which should have an aspect ratio of 16:9 ...

<div class="youtube rounded mb-4" data-embed="roLwUDzRaxs" style="background-image:url('path/to/image.jpg')">YouTube Video</div>

Javascript

document.addEventListener("DOMContentLoaded", function() {
  var youTubeVideos = document.querySelectorAll('.youtube');
  for (var i = 0; i < youTubeVideos.length; i++) {
    var yt_thumbnail = "https://img.youtube.com/vi_webp/" + youTubeVideos[i].dataset.embed + "/maxresdefault.webp";
    youTubeVideos[i].removeAttribute("style");
    youTubeVideos[i].setAttribute("itemprop", "video");
    youTubeVideos[i].setAttribute("itemscope", "");
    youTubeVideos[i].setAttribute("itemtype", "http://schema.org/VideoObject");
    youTubeVideos[i].innerHTML = '<meta itemprop="thumbnailURL" content="' + yt_thumbnail + '">'
      + '<meta itemprop="embedURL" content="https://www.youtube-nocookie.com/embed/' + youTubeVideos[i].dataset.embed + '">'
      + '<img loading="lazy" class="bg-body-tertiary ratio ratio-16x9 youtube" alt="YouTube Video" style="background-image:url(' + yt_thumbnail + ');cursor:pointer" src="' + yt_thumbnail + '">'
      + '<div class="play-button"></div><div class="privacy-info d-none">Third Party Content from YouTube</div>';
    youTubeVideos[i].addEventListener("click", function() {
      this.innerHTML = '<div class="bg-body-tertiary ratio ratio-16x9"><iframe class="border-0" loading="lazy" allow="accelerometer;autoplay;encrypted-media;gyroscope;picture-in-picture" src="https://www.youtube-nocookie.com/embed/' + this.dataset.embed + '?showinfo=0&autoplay=1&modestbranding=1" allowFullScreen></iframe></div>';
    });
  }
});

CSS

.youtube {
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  aspect-ratio: 16/9 !important;
  width: 100%;
  background-size: cover !important;
  background-position: center center !important;
  background-repeat: no-repeat !important;
}

.youtube:hover {
  cursor: pointer;
}

.youtube:hover .play-button {
  background: red;
  opacity: 1;
}

.youtube .play-button {
  font-size: 1.1em;
  position: absolute;
  background: #3b3636;
  opacity: 1;
  border-radius: 50% / 10%;
  height: 3em;
  width: 4em;
  padding: 0;
  text-align: center;
  text-indent: 0.1em;
  z-index: 1;
  scale: 1;
}

.youtube .play-button::before {
  background: inherit;
  border-radius: 5% / 50%;
  content: "";
  position: absolute;
  left: -5%;
  right: -5%;
  top: 9%;
  bottom: 9%;
}

.youtube .play-button::after {
  border-style: solid;
  border-width: 1em 0 1em 1.732em;
  border-color: transparent transparent transparent rgba(255, 255, 255, 0.75);
  content: " ";
  font-size: 0.75em;
  height: 0;
  margin: -1em 0 0 -0.75em;
  top: 50%;
  position: absolute;
  width: 0;
}

.youtube .privacy-info {
  position: absolute;
  z-index: 10;
  bottom: 0;
  font-size: 0.8rem;
  background: #3b3636;
  color: #fff !important;
  padding: 0.25rem 0.5rem;
  border-top-left-radius: 0.5rem;
  border-top-right-radius: 0.5rem;
}
Share:

Comments

Sign in to join the conversation.

No comments yet. Be the first!

More from Tips & Tricks