0

我使用以下代码以模态显示嵌入的视频,但是当我单击关闭按钮时,背景中的音频不会停止。对 Stack Overflow 上提供的解决方案进行了很多尝试,但没有任何帮助。

HTML 代码:

  <div class="container" id="photocontainer">
<div class="row pt-5 ">
    <div class="col-sm-3" id="photocol">
       <img src="img/Cinematography/1Jacinta-Tyler.jpg" style="width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor"> 
    </div>
    <div class="col-sm-3" id="photocol">
       <img src="img/Cinematography/2Amanda-Mark.jpg" style="width:100%" onclick="openModal();currentSlide(2)" class="hover-shadow cursor"> 
    </div>

<div id="myModal" class="modal">
    <span class="close cursor" onclick="closeModal();">&times;</span>
    <div class="modal-content">
        <div class="mySlides" >
            <iframe src="player.vimeo.com/video/…; class="mt-5" width="740" height="316" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
            <p>
                <a href="vimeo.com/255103005"; id="clr">
                    <b>Jacinta &amp; Tyler</b>
                </a>
            By <a href="vimeo.com/beautifullifestudios"; id="clr">Beautiful Life Studios</a>
            .</p> 
       </div>

脚本:

<script>
function openModal() {
  document.getElementById('myModal').style.display = "block";
}

function closeModal() {
  document.getElementById('myModal').style.display = "none";
}

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("demo");
  var captionText = document.getElementById("caption");
  if (n > slides.length) {slideIndex = 1}
  if (n < 1) {slideIndex = slides.length}
   for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";
  }

  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
    slides[slideIndex-1].style.display = "block";
  dots[slideIndex-1].className += " active";
  captionText.innerHTML = dots[slideIndex-1].alt;
}

</script>
4

2 回答 2

1

首先,澄清您使用的是 <video>标签还是<iframe>标签。如果您使用<video>标签,您可以分配id给您的视频,例如<video id="my_video">,然后使用findElementById,您可以在函数中使用play()pause()方法。closeModal()

否则,如果您正在使用<iframe>,请先提供id类似的内容:

<iframe id="my_vide" class="embed-responsive-item" src="link_to_some_video"allowfullscreen></iframe>

你可以这样做:

function closeModal() {
    var video = document.getElementById("vid");
    document.getElementById('myModal').style.display = "none";
    document.getElementById('vid').src = '';
}

如果您有多个视频,您可以将源存储在数组中,并且可以通过简单地为每张幻灯片传递索引并将其设置为您的<iframe>.

这是代码示例:

// Video src
 var video_src = [
           "https://www.youtube.com/embed/sdUUx5FdySs?autoplay=1",
           "https://www.youtube.com/embed/YE7VzlLtp-4?autoplay=1"
];

var current_video = "";

var frame = document.getElementById('vid');

// slides
var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
  current_video = video_src[n-1];
  frame.src = current_video;

}
于 2018-07-19T20:48:08.027 回答
1

你可以在模态关闭时暂停视频..

 function closeModal() {
  document.getElementById('myModal').style.display = "none";

  document.getElementById("youVideoId").pause();
}
于 2018-07-19T20:11:43.077 回答