如何仅在出现框时加载 youtube 嵌入式视频

拉胡尔·夏尔马

在我的代码中,当用户单击按钮时,youtube 视频会出现在弹出窗口中。

这是我的小提琴

我希望视频仅在有人单击按钮并出现弹出窗口时加载,并在有人关闭框时停止播放。

目前,我的代码只是隐藏显示,仅此而已。

HTML:

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

CSS:

/* 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;
}

Javascript:

// 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";
   }
 }
渴望

添加?autoplay=1到您的 iframe src

复位src停止录像。

iframe仅在单击按钮时加载源,而不是在文档加载时加载它,您应该srcplayVideo()函数设置属性

为此,您可以向iframe( data-src)添加一个新属性,该属性将存储视频 URL,然后在需要时访问它的值。

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

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

function playVideo() {
  var video = document.getElementById('video');
  var src = video.dataset.src;

  video.src = src + '?autoplay=1';
}

function resetVideo() {
  var video = document.getElementById('video');
  var src = video.src.replace('?autoplay=1', '');

  video.src = '';
  video.src = src;
}

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

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

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

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
    resetVideo();
  }
}
/* 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="video" class="trailervideo" width="560" height="315" src="https://www.youtube.com/embed/TDwJDRbSYbw" frameborder="0" allowfullscreen></iframe>
    </div>
  </div>
</div>

如果您无法播放上面代码段中的视频,请参阅小提琴

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

嵌入式YouTube视频无法加载

如何恢复嵌入式YouTube视频?

如何杀死正在播放的嵌入式youtube视频

如何在WKWebView中启用嵌入式YouTube视频?

如何检查YouTube视频是否允许嵌入式播放

如何使嵌入式youtube视频和woff文件的加载速度更快

Youtube 嵌入式视频 (iframe) 仅在 Safari 中表现怪异

使用YTPlayerView在iOS中播放嵌入式YouTube视频失败,出现限制错误

向右浮动时,嵌入式youtube视频将无法播放

使用HTMl5,如何对嵌入式YouTube视频进行居中和居中?

如何在全屏模式下使 youtube 嵌入式视频保持居中

如何将嵌入式YouTube视频的框架(通过iframe)写入画布?

如何从UIWebView嵌入式YouTube视频播放中接收NSNotifications

关闭模态后,如何停止播放嵌入式iframe youtube视频?

如何从嵌入式YouTube播放列表中禁用“相关视频”

如何在嵌入式YouTube视频中启用自动字幕?

如何在嵌入式YouTube视频上添加按钮?

Selenium C#如何暂停/播放嵌入式youtube视频

改变嵌入式Youtube视频的宽高比

YouTube嵌入式视频无法启动

Youtube 嵌入式视频白色区域

YouTube嵌入式视频问题(视频不可用)

观察R Shiny中的另一个事件时控制嵌入式YouTube视频

为什么嵌入式YouTube视频上周工作时说“视频不可用”?无需更改代码,Youtube Data V3 API

如何确定何时加载youtube嵌入式iframe

将嵌入式vimeo切换到嵌入式youtube视频

在嵌入式视频上禁用YouTube字幕

URL清理导致刷新嵌入式YouTube视频

URL清理导致刷新嵌入式YouTube视频