1
votes

I want to use code to make my site load YouTube Embeds faster. The code displays the thumbnail of a YouTube video and only when you click the thumbnail the video player will load. This is handy because YouTube embeds are big and slow your page down. And this way you only load the video player when you are really going to watch the video.

I want to use the code to embed YouTube playlists instead of single videos. But the problem i have, is that i cannot find a way to automatically display the last thumbnail of the playlist. So the thumbnail of the video that was last added to the playlist. I did manage to display the playlist when you click the (default) thumbnail so that is no problem anymore.

Does anybody have any suggestions about how to display the latest thumbnail?

I would be very grateful.

This is the code: https://codepen.io/DeanAmsterdam/pen/pozWxjW -I changed HTML line 1, from a youtube video ID to a playlist ID. -I changed JS Line 23, from "https://www.youtube.com/embed/ID?autoplay=1"; TO "https://www.youtube.com/embed/videoseries?list=ID";

document.addEventListener("DOMContentLoaded",
    function() {
        var div, n,
            v = document.getElementsByClassName("youtube-player");
        for (n = 0; n < v.length; n++) {
            div = document.createElement("div");
            div.setAttribute("data-id", v[n].dataset.id);
            div.innerHTML = labnolThumb(v[n].dataset.id);
            div.onclick = labnolIframe;
            v[n].appendChild(div);
        }
    });

function labnolThumb(id) {
    var thumb = '<img src="https://i.ytimg.com/vi/ID/hqdefault.jpg">',
        play = '<div class="play"></div>';
    return thumb.replace("ID", id) + play;
}

function labnolIframe() {
    var iframe = document.createElement("iframe");
    var embed = "https://www.youtube.com/embed/videoseries?list=ID";
    iframe.setAttribute("src", embed.replace("ID", this.dataset.id));
    iframe.setAttribute("frameborder", "0");
    iframe.setAttribute("allowfullscreen", "1");
    this.parentNode.replaceChild(iframe, this);
}

i also found a second piece of code that does the same thing, i made changes on HTML line 1 and JS line 21. The changes are the same as on the other code: https://codepen.io/DeanAmsterdam/pen/gOYoLLY

I am only just a simple Wordpress user so please forgive me if i made any mistakes in explaining.

Thank You.

1

1 Answers

1
votes

You can embed it like this:

Html:

 <div class="youtube-player" data-id="XZ6y84lvGp8" data-related="1" data-control="1" data-info="1" data-fullscreen="1" style="width:100%;display: block; position: relative;cursor: pointer;max-height:1080px;height:100%; overflow:hidden;padding-bottom:56.25%;margin:0 auto"> <img src="//i.ytimg.com/vi/XZ6y84lvGp8/hqdefault.jpg" style="bottom: -100%; display: block; left: 0; margin: auto; max-width: 100%; width: 100%;height:auto; position: absolute; right: 0; top: -100%;"> <div style="height: 72px; width: 72px; left: 50%; top: 50%; margin-left: -36px; margin-top: -36px; position: absolute; background: url('http://i.imgur.com/TxzC70f.png') no-repeat;"></div> </div> 

Javascript:

<script> (function() { var v = document.getElementsByClassName("youtube-player"); for (var n = 0; n < v.length; n++) { v[n].onclick = function () { var iframe = document.createElement("iframe"); iframe.setAttribute("src", "//www.youtube.com/embed/" + this.dataset.id + "?autoplay=1&autohide=2&border=0&wmode=opaque&enablejsapi=1&rel="+ this.dataset.related +"&controls="+this.dataset.control+"&showinfo=" + this.dataset.info); iframe.setAttribute("frameborder", "0"); iframe.setAttribute("id", "youtube-iframe"); iframe.setAttribute("style", "width: 100%; height: 100%; position: absolute; top: 0; left: 0;"); if (this.dataset.fullscreen == 1){ iframe.setAttribute("allowfullscreen", ""); } while (this.firstChild) { this.removeChild(this.firstChild); } this.appendChild(iframe); }; } })(); </script>