带视频的模态弹出窗口,关闭后保持播放音频

时间:2016-11-30 13:42:16

标签: javascript html css

我在我的网站上制作了一个弹出视频按钮。 但是当我关闭它或点击模态输出空间时,音频仍然会继续。

我该如何解决这个问题? 我尝试了一些javascript函数,但我认为我做错了...

谢谢!



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

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

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

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

// When the user clicks on <span> (x), close the modal
span.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";
  }
}
&#13;
<button type="button" class="button" id="myBtn">Watch Video</button>
<div id="myModal" class="modal">

<!-- Modal content -->
<div class="modal-content">
  <div class="modal-header">
    <span class="close">×</span>
  </div>
  <div class="modal-body">
<iframe width="560" height="315" src="https://www.youtube.com/embed/QH2-TGUlwu4" frameborder="0" allowfullscreen></iframe>
  </div>
  <div class="modal-footer">
  </div>
</div>

</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

最简单的方法是重新分配iframe视频src值,使其重置并停止。例如。为iframe指定示例id="yplayer"的ID,然后使用以下语句停止并重新加载视频。

document.getElementById('yplayer').src = document.getElementById('yplayer').src;

第二个选项是使用youtube api函数,这将涉及更多的代码处理。 Youtube API Docs

希望这有帮助。