0x0049 icon

YT play

0x0049 | PRO | 07/16/15 07:01:40 PM UTC | 0 ⭐ | 159 👁️ | Never ⏰ | []
JavaScript |

690 B

|

None

|

0 👍

/

0 👎

/**
 * 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();
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎