JSFiddle - React, Tailwind, and code Playground
HTML
<div class="container">
<div class="curtain">
<div class="ratio-keeper">
<!-- 1. Video Wrapper Container -->
<div class="embed-youtube " data-video-id="djV11Xbc914">
<!-- 2. The preview button that will contain the Play icon -->
<button class="embed-youtube-play" type="button" aria-label="Open"></button>
</div>
</div>
</div>
<div class="curtain">
<div class="ratio-keeper">
<!-- 1. Video Wrapper Container -->
<div class="embed-youtube " data-video-id="djV11Xbc914">
<!-- 2. The preview button that will contain the Play icon -->
<button class="embed-youtube-play" type="button" aria-label="Open"></button>
</div>
</div>
</div>
<div class="curtain">
<div class="ratio-keeper">
<!-- 1. Video Wrapper Container -->
<div class="embed-youtube " data-video-id="djV11Xbc914">
<!-- 2. The preview button that will contain the Play icon -->
<button class="embed-youtube-play" type="button" aria-label="Open"></button>
</div>
</div>
</div>
</div>
CSS
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
body {
background: white;
}
.container {
position: absolute;
left: 0;
right: 0;
min-height: 100%;
padding: 8px 8px;
}
.curtain {
margin:auto auto 20px;
max-width: 640px;
border: 21px solid;
border-radius: 12px;
border-color: #000 #101010 #000 #101010;
position: relative;
}
.curtain::before {
content: '';
position: absolute;
top: -2px;
left: -2px;
right: -2px;
bottom: -2px;
background: #0a0a0a;
border: 1px solid;
border-color: #000 #101010 #000 #101010;
}
.curtain::after {
content: "";
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
outline: 1px solid #333;
pointer-events: none;
}
.curtain.slide::after {
outline: 1px solid #0059dd;
transition: outline 2s ease-in;
}
.ratio-keeper {
position: relative;
height: 0;
padding-top: 56.25%;
margin: auto;
overflow: hidden;
}
.embed-youtube {
background-color: #000;
margin-bottom: 100px;
}
.embed-youtube.five {
margin-bottom: 0;
}
.embed-youtube img {
width: 100%;
top: -16.84%;
left: 0;
opacity: 1;
}
/*.embed-youtube img,
.embed-youtube .embed-youtube-play {
cursor: default;
}*/
.embed-youtube img,
.embed-youtube iframe,
.embed-youtube .embed-youtube-play,
.embed-youtube .embed-youtube-play:before {
position: absolute;
}
.embed-youtube {
margin-bottom: 0;
}
.slide img {
animation: fadeOutBack 3s forwards 0s;
}
@keyframes fadeOutBack {
0% {
opacity: 1;
visibility: visible;
}
99.9% {
opacity: 0;
visibility: hidden;
/*pointer-events: none;*/
}
100% {
opacity: 0;
visibility: hidden;
}
}
.embed-youtube iframe {
height: 100%;
width: 100%;
top: 0;
left: 0;
animation: fade 3s ease-in 0s forwards;
}
@keyframes fade {
0% {
opacity: 0;
visibility: hidden;
}
100% {
opacity: 1;
visibility: visible;
}
}
.embed-youtube .embed-youtube-play {
-webkit-appearance: none;
...
JavaScript
(function initCover() {
/* function show(el) {
el.classList.remove("hide");
}*/
/* function hide(el) {
el.classList.add("hide");
}*/
function openCurtain(cover) {
/* hide(cover);*/
const curtain = document.querySelector(".curtain");
curtain.classList.add("slide");
return curtain;
}
/*function showVideo(curtain) {
const thewrap = curtain.parentElement.querySelector(".wrap");
show(thewrap);
}*/
function coverClickHandler(evt) {
const cover = evt.currentTarget;
const curtain = openCurtain(cover);
/* showVideo(curtain);*/
cover.dispatchEvent(new Event("afterClick"));
}
const covers = document.querySelectorAll(".embed-youtube");
covers.forEach(function addHandler(cover) {
cover.addEventListener("click", coverClickHandler);
});
}());
(function () {
let YouTubeContainers = document.querySelectorAll(".embed-youtube");
// Iterate over every YouTube container you may have
for (let i = 0; i < YouTubeContainers.length; i++) {
let container = YouTubeContainers[i];
let imageSource = "https://img.youtube.com/vi/" + container.dataset.videoId + "/sddefault.jpg";
// Load the Thumbnail Image asynchronously
let image = new Image();
image.src = imageSource;
image.addEventListener("load", function () {
container.appendChild(image);
});
// When the user clicks on the container, load the embedded YouTube video
container.querySelector("button").addEventListener("click", function () {
let iframe = document.createElement("iframe");
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("allowfullscreen", "");
iframe.setAttribute("allow", "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture");
// Important: add the autoplay GET parameter, otherwise the user would need to click over the YouTube video again to play it
iframe.setAttribute("src",...