javascript - 如何仅在单击按钮时加载 iframe

标签 javascript jquery html css iframe

仅当单击“观看视频”按钮时,我才需要 YouTube 视频 Iframe。目前,当页面加载时,视频 iframe 在后台加载。

注意:我的意思不是在单击按钮时播放视频。我的意思是,在单击按钮时加载 iframe

https://jsfiddle.net/kz0xxe22/

// Get the modal
var modal = document.getElementById('trailerdivbox');

// Get the button that opens the modal
var btn = document.getElementById("watchbutton");

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

var trailerbox = document.getElementById("close");

trailerbox.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
/* Watch Trailer Button CSS */

#watchbutton {
  background-color: #f2f2f2;
  color: red;
  font-weight: 600;
  border: none;
  /* This one removes the border of button */
  padding: 10px 12px;
}

#watchbutton:hover {
  background-color: #e2e2e2;
  cursor: pointer;
}

#trailerdivbox {
  display: none;
  width: 100%;
  height: 100%;
  position: fixed;
  overflow: auto;
  /* Enable Scrolling */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}

.videoWrapper {
  position: relative;
  padding-bottom: 56.25%;
  /* 16:9 */
  padding-top: 25px;
  height: 0;
}

.videoWrapper iframe {
  position: absolute;
  max-width: 560px;
  max-height: 315px;
  width: 95%;
  height: 95%;
  left: 0;
  right: 0;
  margin: auto;
}
<button id="watchbutton">Watch Trailer &#9658</button>

<div id="close">
  <div id="trailerdivbox">
    <div class="videoWrapper">
      <iframe class="trailervideo" width="560" height="315" src="https://www.youtube.com/embed/TDwJDRbSYbw" frameborder="0" allowfullscreen></iframe>
    </div>
  </div>
</div>

最佳答案

您可以将src属性更改为data-src,然后当您单击按钮时,将src设置为data -src

// Get the modal
var modal = document.getElementById('trailerdivbox');

// Get the button that opens the modal
var btn = document.getElementById("watchbutton");

var trailer = document.getElementById('trailervideo');

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
  trailer.setAttribute('src', trailer.getAttribute('data-src'));
}

var trailerbox = document.getElementById("close");

trailerbox.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
/* Watch Trailer Button CSS */

#watchbutton {
  background-color: #f2f2f2;
  color: red;
  font-weight: 600;
  border: none;
  /* This one removes the border of button */
  padding: 10px 12px;
}

#watchbutton:hover {
  background-color: #e2e2e2;
  cursor: pointer;
}

#trailerdivbox {
  display: none;
  width: 100%;
  height: 100%;
  position: fixed;
  overflow: auto;
  /* Enable Scrolling */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}

.videoWrapper {
  position: relative;
  padding-bottom: 56.25%;
  /* 16:9 */
  padding-top: 25px;
  height: 0;
}

.videoWrapper iframe {
  position: absolute;
  max-width: 560px;
  max-height: 315px;
  width: 95%;
  height: 95%;
  left: 0;
  right: 0;
  margin: auto;
}
<button id="watchbutton">Watch Trailer &#9658</button>


<div id="close">
  <div id="trailerdivbox">
    <div class="videoWrapper">
      <iframe id="trailervideo" class="trailervideo" width="560" height="315" data-src="https://www.youtube.com/embed/TDwJDRbSYbw" frameborder="0" allowfullscreen></iframe>
    </div>
  </div>
</div>

关于javascript - 如何仅在单击按钮时加载 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44266225/

相关文章:

javascript - Codeigniter - 首先确认 anchor

javascript - 单击链接时删除 Div

javascript - 连续 Bootstrap 七列

javascript - 如何找出一个 HTML 元素是否在另一个 HTML 元素后面?

HTML 边距问题 - 正文可滚动超过声明的宽度

javascript - 如何解析 JSON 数组

javascript - 将标记图标添加到 svg map

javascript - Highcharts 图例与 X 轴重叠

javascript - 当焦点位于 2 个文本框中的特定文本框时按下 Enter 键时发出警报消息

javascript - 从文件夹中提取所有图像并创建 Bootstrap 轮播/幻灯片