Skip to content

Instantly share code, notes, and snippets.

@romanoffs
Last active December 12, 2018 16:37
Show Gist options
  • Save romanoffs/ec9b7c27b7df03c319d1efd133d3db97 to your computer and use it in GitHub Desktop.
Save romanoffs/ec9b7c27b7df03c319d1efd133d3db97 to your computer and use it in GitHub Desktop.
youtube embeded
"use strict";
$(function() {
$(".youtube").each(function() {
$(this).css('background-image', 'url(//i.ytimg.com/vi/' + this.id + '/sddefault.jpg)');
$(this).append($('<div/>', {'class': 'play'}));
$(document).delegate('#'+this.id, 'click', function() {
var iframe_url = "https://www.youtube.com/embed/" + this.id + "?autoplay=1&autohide=1";
if ($(this).data('params')) iframe_url+='&'+$(this).data('params');
var iframe = $('<iframe/>', {'frameborder': '0', 'src': iframe_url, 'width': $(this).width(), 'height': $(this).height() })
$(this).replaceWith(iframe);
});
});
});
// Js only
// var videos = // get elements
var nb_videos = videos.length;
for (var i = 0; i < nb_videos; i++) {
videos[i].style.backgroundImage = 'url(http://i.ytimg.com/vi/' + videos[i].id + '/sddefault.jpg)';
var play = document.createElement("div");
play.setAttribute("class", "play");
videos[i].appendChild(play);
videos[i].onclick = function() {
var iframe = document.createElement("iframe");
var iframe_url = "https://www.youtube.com/embed/" + this.id + "?autoplay=1&autohide=1&rel=0&controls=0&enablejsapi=1";
if (this.getAttribute("data-params"))
iframe_url += '&' + this.getAttribute("data-params");
iframe.setAttribute("src", iframe_url);
iframe.setAttribute("frameborder", '0');
iframe.style.width = this.style.width;
iframe.style.height = this.style.height;
this.parentNode.replaceChild(iframe, this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment