Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Load a YouTube video after clicking a thumbnail. Use like so:
- * <img id="youtubeid" onclick="playYouTubeVideo(this)" src="thumbnail.jpg">
- */
- function playYouTubeVideo(element) {
- // Create an iFrame with autoplay set to true so it starts as soon as we insert it.
- var iframe = document.createElement('iframe');
- iframe.setAttribute('src', 'https://www.youtube.com/embed/' + element.id + '?autoplay=1&autohide=1&showinfo=0&modestbranding=1');
- // Replace the thumbnail with YouTube player.
- var parent = element.parentNode;
- parent.replaceChild(iframe, element);
- // I recommended using FitVids after inserting the iFrame if responsive video is needed.
- jQuery(parent).fitVids();
- }
Advertisement