Full screen/background video from YouTube using jQuery Full screen/background video from YouTube using jQuery

I was searching the web for some neat ideas and I came across this intriguing jQuery plugin that allows you to embed a YouTube video on your website. No, not the standard player, but instead the video is your background. Similar to how you would use an image or color, the YouTube video plays automatically in full screen as the background of your website; here is a demo of the plugin.


Embed YouTube video as your webpage's background

The YouTube background video plugin requires jQuery (it is a plugin of course), so for the purposes of this example I will not download it and just reference jQuery's CDN.

To start with you'll want to grab your favorite YouTube video or a video your website has made that you want to make the transparent background. This page would make a great landing page for a link from an email campaign providing immediate video playing upon loading.

Once you have your video URL, you need to setup a div tag that contains the information the plugin will use to load the video in the background and begin playing:


<div id="bgVideo" class="player"
data-property="{
videoURL:'https://youtu.be/6Wcw8Xg8Vb0',
containment:'body',
autoPlay:true,
mute:false,
startAt:0,
opacity:1
}">Your video</div>

All of the required information is contained inside the attribute called data-property as a JSON object. In the example above you can see options such as: auto play, mute, starting position and of course the video URL. There is a great opportunity to convert this code into a jQuery HTML template as I describe in another article.

To finish this off, you need a bit of JavaScript array and loading of the CSS to make the video the background:


<link href="css/YTPlayer.css" media="all" rel="stylesheet" type="text/css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="inc/jquery.mb.YTPlayer.js"></script>
jQuery(function(){
jQuery("#bgVideo").YTPlayer();
});

Be sure to download the files from the GitHub repository. Here is the final source code:


<link href="css/YTPlayer.css" media="all" rel="stylesheet" type="text/css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="inc/jquery.mb.YTPlayer.js"></script>
<div id="bgVideo" class="player"
data-property="{
videoURL:'https://youtu.be/6Wcw8Xg8Vb0',
containment:'body',
autoPlay:true,
mute:false,
startAt:0,
opacity:1
}">Your video</div>
jQuery(function(){
jQuery("#bgVideo").YTPlayer();
});

Published on Feb 17, 2020

Tags: JavaScript | jQuery Tutorial

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.