仅在出现框时才使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

reset src停止视频。

仅在按钮上加载iframe源,而不是将其加载到文档加载上,您应该在playVideo()函数上设置src属性。

为此,您可以在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>

如果您不能在上面的摘要中播放视频,请参阅小提琴。

检查YouTube API的API DOC

player.stopVideo()

所以在jquery

$('#ID').get(0).stopVideo();

尝试此

<a id="stop" href="#">Stop</a>
<iframe id="popup-youtube-player" width="640" height="360" src="http://www.youtube.com/embed/geTgZcHrXTc?enablejsapi=1&version=3&playerapiid=ytplayer" frameborder="0" allowfullscreen="true" allowscriptaccess="always"></iframe>

$('#stop').on('click', function() {
    //$('#popup-youtube-player').stopVideo();
$('#popup-youtube-player')[0].contentWindow.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');    });

最新更新