JSFiddle - React, Tailwind, and code Playground
HTML
<section id="home-banner-box" class="home-banner loading">
<div class="image video-slide" style="background-image: url($ImageURL)">
<div class="video-background">
<div class="video-foreground" id="YouTubeBackgroundVideoPlayer">
</div>
</div>
</div>
</section>
<script async src="https://www.youtube.com/iframe_api"></script>
<script async src="video.js"></script>
<script>
onYouTubeIframeAPIReady()
</script>
CSS
.home-banner .slide .video-slide {
background-color: #000; }
.home-banner.loading .video-background {
opacity: 0; }
.video-background {
position: absolute;
top: 50%;
left: 0;
padding-top: 56.25%;
width: 100%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
-webkit-transition: 2s opacity ease;
transition: 2s opacity ease;
opacity: 1; }
.video-foreground,
.video-background iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none; }
JavaScript
function onYouTubeIframeAPIReady() {
var player;
player = new YT.Player('YouTubeBackgroundVideoPlayer', {
videoId: '{$loadYouTubeVideoID}', // YouTube Video ID
width: 1280, // Player width (in px)
height: 720, // Player height (in px)
playerVars: {
playlist: '{$loadYouTubeVideoID}',
autoplay: 1, // Auto-play the video on load
autohide: 1,
disablekb: 1,
controls: 0, // Hide pause/play buttons in player
showinfo: 0, // Hide the video title
modestbranding: 1, // Hide the Youtube Logo
loop: 1, // Run the video in a loop
fs: 0, // Hide the full screen button
autohide: 0, // Hide video controls when playing
rel: 0,
enablejsapi: 1
},
events: {
onReady: function(e) {
e.target.mute();
e.target.setPlaybackQuality('hd1080');
},
onStateChange: function(e) {
if(e && e.data === 1){
var videoHolder = document.getElementById('home-banner-box');
if(videoHolder && videoHolder.id){
videoHolder.classList.remove('loading');
}
}else if(e && e.data === 0){
e.target.playVideo()
}
}
}
});
}