使YouTube嵌入(播放列表)加载速度更快



我想使用代码让我的网站更快地加载YouTube嵌入。该代码显示 YouTube 视频的缩略图,只有当您单击缩略图时,视频播放器才会加载。这很方便,因为YouTube嵌入很大,会减慢您的页面速度。这样,您只会在真正要观看视频时才加载视频播放器。

我想使用代码嵌入YouTube播放列表而不是单个视频。但是我遇到的问题是我找不到自动显示播放列表最后一个缩略图的方法。因此,上次添加到播放列表的视频的缩略图。当您单击(默认(缩略图时,我确实设法显示播放列表,因此不再有问题。

有人对如何显示最新的缩略图有任何建议吗?

我将不胜感激。

这是代码:https://codepen.io/DeanAmsterdam/pen/pozWxjW - 我将HTML第1行从YouTube视频ID更改为播放列表ID。 -我更改了JS第23行,从"https://www.youtube.com/embed/ID?autoplay=1";到"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);
}

我还找到了第二段做同样事情的代码,我对 HTML 第 1 行和 JS 第 21 行进行了更改。更改与其他代码相同:https://codepen.io/DeanAmsterdam/pen/gOYoLLY

我只是一个简单的Wordpress用户,所以如果我在解释时犯了任何错误,请原谅我。

谢谢。

您可以像这样嵌入它:

目录:

<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>

最新更新