【问题标题】:Javascript - page with 30+ embedded youtube videos - load iframe on clickJavascript - 包含 30 多个嵌入式 youtube 视频的页面 - 点击加载 iframe
【发布时间】:2021-02-04 09:25:17
【问题描述】:

我想寻求帮助。我有从 youtube 嵌入的 30 多个视频的网页。我想在用户点击按钮时显示 1-2 个视频和其余视频。

目前,嵌入的视频是隐藏的,但我感觉它们也在后台加载。

点击按钮后显示视频和从 Youtube 加载的最佳方式是什么?

我当前的代码:

<center>
<button onclick="myFunction('kataguruma')" class="myButton">Show Kata-guruma</button> <br><br></center>
<p> </p>
<div id="kataguruma" style="display: none;">
<center>
<iframe src="https://www.youtube.com/embed/YrE62Dzg4oM" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" width="560" height="315" frameborder="0">
</iframe>
</center>
</div>

<script>// <![CDATA[
function myFunction(el) {
  var x = document.getElementById(el);
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
// ]]></script>

【问题讨论】:

  • 创建元素,但稍后使用您的事件填充 src。

标签: javascript html


【解决方案1】:

我不确定我是否正确理解了您的问题,因此如果我错了,请纠正我。 假设您想在用户单击视频时播放视频。只需删除allow= "autoplay"

<iframe src="https://www.youtube.com/embed/YrE62Dzg4oM" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" width="560" height="315" frameborder="0">

如果您希望视频在页面加载时自动播放,您不仅需要添加allow="autoplay",还需要在嵌入链接源的末尾添加?autoplay=1,就像这样。

<iframe src="https://www.youtube.com/embed/YrE62Dzg4oM?autoplay=1" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" width="560" height="315" frameborder="0">

如果您只想在用户按下按钮时加载和插入 iframe。您可以在下面找到示例代码,看看它是否回答了您的问题。

//list of links 
let links = new Array ("https://www.youtube.com/embed/YrE62Dzg4oM",
                        "https://www.youtube.com/embed/YrE62Dzg4oM");

function myFunction(el) {
  var x = document.getElementById(el);
  if (x.style.display === "none") {
    x.style.display = "block";

    //loop through all the link which are in the list 
    for(i = 0 ; i < links.length ; i++){
    var ifrm = document.createElement('iframe');
    ifrm.setAttribute('id', "ifrm"+i ); // assign an id

    // assign url
    ifrm.setAttribute('src', links[i]);
    ifrm.setAttribute('width', '560');
    ifrm.setAttribute('height', '315');
    // Append the iframe to the div
    document.getElementById("kataguruma").appendChild(ifrm);
    }
    
  } else {
    x.style.display = "none";
    // remove all the iframes in the div when the button is pressed again 
    var node = document.getElementById('kataguruma');
    node.innerHTML = "";
  }
}
<center>
<button onclick="myFunction('kataguruma')" class="myButton">Show Kata-guruma</button> <br><br></center>
<p> </p>
 <iframe src="https://www.youtube.com/embed/YrE62Dzg4oM" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" width="560" height="315" frameborder="0" style="">
</iframe>

 <iframe src="https://www.youtube.com/embed/YrE62Dzg4oM" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" width="560" height="315" frameborder="0" style="">
</iframe>

<div id="kataguruma" style="display: none;">

</div>

【讨论】:

  • 感谢谭一静的回复。整个想法是尽可能快地加载页面。我了解(也许我错了)iframe 会转到源并提取一些数据。但我希望这种行为仅适​​用于 2 个视频。其余的将在用户单击按钮后加载。默认情况下,视频是“显示:无”
  • 嗨,我已经在上面编辑了我的答案。 iframe 只会在用户按下按钮时从存储在 JS 中的数组生成和加载。如果您想拥有 30 个视频,只需将 30 个链接添加到数组即可。如果这解决了您的问题,您可以将我的答案标记为正确。
猜你喜欢
  • 2011-11-27
  • 2018-01-18
  • 2013-10-19
  • 2011-12-05
  • 2015-11-30
  • 2015-06-24
  • 2021-02-11
  • 2018-03-16
  • 2018-02-07
相关资源
最近更新 更多