php Light YouTube嵌入式 Posted 2021-05-05
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Light YouTube嵌入式相关的知识,希望对你有一定的参考价值。
/* Light YouTube Embeds by @labnol */
/* Web: http://labnol.org/?p=27941 */
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/ID?autoplay=1";
iframe.setAttribute("src", embed.replace("ID", this.dataset.id));
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("allowfullscreen", "1");
this.parentNode.replaceChild(iframe, this);
}
.youtube-player {
position: relative;
padding-bottom: 56.23%;
/* Use 75% for 4:3 videos */
height: 0;
overflow: hidden;
max-width: 100%;
background: #000;
margin: 5px;
iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 100;
background: transparent;
}
img {
bottom: 0;
display: block;
left: 0;
margin: auto;
max-width: 100%;
width: 100%;
position: absolute;
right: 0;
top: 0;
border: none;
height: auto;
cursor: pointer;
-webkit-transition: .4s all;
-moz-transition: .4s all;
transition: .4s all;
&:hover {
-webkit-filter: brightness(75%);
}
}
.play {
height: 72px;
width: 72px;
left: 50%;
top: 50%;
margin-left: -36px;
margin-top: -36px;
position: absolute;
background: url("//i.imgur.com/TxzC70f.png") no-repeat;
cursor: pointer;
}
}
<!--only take ID https://www.youtube.com/watch?v=2L9vFNMvIBE -->
<!--only take ID-> 2L9vFNMvIBE -->
<div class="youtube-player" data-id="VIDEO_ID"></div>
以上是关于php Light YouTube嵌入式的主要内容,如果未能解决你的问题,请参考以下文章
更改 YouTube 视频嵌入的背景颜色
嵌入式 youtube 视频未通过 iframe 和 php 显示
在 PHP 字符串中查找 youtube 链接并将其转换为嵌入代码?
php 将YouTube网址转换为嵌入网址
php 设置参数 - YouTube嵌入
php Youtube或Vimeo嵌入