I using the following code to show the embedded video in modal but when I click the close button the audio in the background doesn't stop. Tried a lot with the provided solutions on Stack Overflow but nothing helped me.
HTML Code :
<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();">×</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 & Tyler</b>
</a>
By <a href="vimeo.com/beautifullifestudios"; id="clr">Beautiful Life Studios</a>
.</p>
</div>
Script:
<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>
First, clarify either you are using <video>
tag or <iframe>
tag. If you are using <video>
tag, you can assign id
to your video e.g <video id="my_video">
and then by using findElementById
, you can use play()
and pause()
methods in your closeModal()
function.
Otherwise, if you are using <iframe>
, first provide it an id
like:
<iframe id="my_vide" class="embed-responsive-item" src="link_to_some_video"allowfullscreen></iframe>
you can do it as follows:
function closeModal() {
var video = document.getElementById("vid");
document.getElementById('myModal').style.display = "none";
document.getElementById('vid').src = '';
}
If you have multiple videos, you can store sources in arrays and can get them by simply passing index for each slide and setting it as a source to your <iframe>
.
Here is the code example:
// 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;
}