Skip to content

Instantly share code, notes, and snippets.

@aradnom
Created July 29, 2014 21:28
Show Gist options
  • Save aradnom/512becc133c0253608d3 to your computer and use it in GitHub Desktop.
Save aradnom/512becc133c0253608d3 to your computer and use it in GitHub Desktop.
YouTube API example wrapping existing iframe elements.
/* Youtube */
window.onYouTubeIframeAPIReady = function () {
window.youtube_players = [];
$('.video_container.youtube').each( function () {
// Only set the play functionality up if the video has a thumbnail
if ( $(this).find('.video_thumbnail').length ) {
var thumbnail = $(this).find('.video_thumbnail'),
play = $(this).find('.video_play'),
player = new YT.Player( $(this).children('iframe').attr('id') );
window.youtube_players.push( player );
var thumbnail_height = thumbnail.find('img').height();
$(this).height(thumbnail_height);
// Play button
play.click( function () {
player.playVideo();
thumbnail.fadeTo( 200, 0, function () {
thumbnail.css( 'z-index', 0 );
});
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment