JSFiddle - React, Tailwind, and code Playground
by ken tsai
HTML
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<div class="bg-youtube"></div>
JavaScript
/*
* YoutubeBackground - A wrapper for the Youtube API - Great for fullscreen background videos or just regular videos.
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
*
* Version: 1.0.1
*
*/
// Chain of Responsibility pattern. Creates base class that can be overridden.
if (typeof Object.create !== "function") {
Object.create = function(obj) {
function F() {}
F.prototype = obj;
return new F();
};
}
(function($, window, document) {
var
loadAPI = function loadAPI(callback) {
// Load Youtube API
var tag = document.createElement('script'),
head = document.getElementsByTagName('head')[0];
if(window.location.origin == 'file://') {
tag.src = 'http://www.youtube.com/iframe_api';
} else {
tag.src = '//www.youtube.com/iframe_api';
}
head.appendChild(tag);
// Clean up Tags.
head = null;
tag = null;
iframeIsReady(callback);
},
iframeIsReady = function iframeIsReady(callback) {
// Listen for Gobal YT player callback
if (typeof YT === 'undefined' && typeof window.loadingPlayer === 'undefined') {
// Prevents Ready Event from being called twice
window.loadingPlayer = true;
// Creates deferred so, other players know when to wait.
window.dfd = $.Deferred();
window.onYouTubeIframeAPIReady = function() {
window.onYouTubeIframeAPIReady = null;
window.dfd.resolve( "done" );
callback();
};
} else if (typeof YT === 'object') {
callback();
} else {
window.dfd.done(function( name ) {
callback();
});
}
};
// YTPlayer Object
YTPlayer = {
player: null,
// Defaults
defaults: {
ratio: 16 / 9,
videoId: 'LSmgKRx5pBo',
mute: true,
repeat: true,
width: $(window).width(),
playButtonClass: 'YTPlayer-play',
...