0x0049

YT play

Jul 16th, 2015
166
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Load a YouTube video after clicking a thumbnail. Use like so:
  3.  * <img id="youtubeid" onclick="playYouTubeVideo(this)" src="thumbnail.jpg">
  4.  */
  5. function playYouTubeVideo(element) {
  6.     // Create an iFrame with autoplay set to true so it starts as soon as we insert it.
  7.     var iframe = document.createElement('iframe');
  8.     iframe.setAttribute('src', 'https://www.youtube.com/embed/' + element.id + '?autoplay=1&autohide=1&showinfo=0&modestbranding=1');
  9.  
  10.     // Replace the thumbnail with YouTube player.
  11.     var parent = element.parentNode;
  12.     parent.replaceChild(iframe, element);
  13.  
  14.     // I recommended using FitVids after inserting the iFrame if responsive video is needed.
  15.     jQuery(parent).fitVids();
  16. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment