1
Well, I’m beginner in web pages I got the code below but all the efforts I try I can not make a universal code to close all kinds of iframe close along with the modal, if anyone can help I would appreciate.
// 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";
}
}
body {font-family: Arial, Helvetica, sans-serif;}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 25px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.container {
position: relative;
width: 100%;
overflow: hidden;
padding-top: 56.25%; /* 16:9 Aspect Ratio */
}
.responsive-iframe {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
width: 100%;
height: 100%;
border: none;
}
<!DOCTYPE html>
<html>
<span id="myBtn"><img style="position: absolute; left: 145px; top: 100px;" title="Trailer" src="https://live.staticflickr.com/65535/50535826153_58450a4c61_o.png" alt="Video" width="25" height="25" /></span>
<!-- lightbox container hidden with CSS -->
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<h3>Teste Titulo Video</h3>
<div class="container"><iframe class="responsive-iframe" width="560" height="315" src="http://www.mobypicture.com/embed?url=http://moby.to/xznwce" frameborder="0" allowfullscreen></iframe></div>
</div>
</div>
</body>
</html>
Thank you is what I needed. Vlw even.
– kastenjhonnes
In case you have a video like "<video id="player" class="Responsive-iframe" width="560" height="315" Controls><source src="video.mp4" type="video/mp4"></video>", I added the code "<script> var player; //capture click the close button and for the video Document.querySelector('.close'). addeventlistener('click', Function(){ var media = Document.querySelector("#player"); media.pause(); media.currentTime = 0; }); </script>" to stop the video, but how do I stop the video when clicking outside the modal?
– kastenjhonnes
@kastenjhonnes you call that same
function(){ var media = document.querySelector("#player"); media.pause(); media.currentTime = 0; });within theif (event.target == modal). From what you can understand from your code, every time we click out of the modal, he gets into thisif, then just call that function inside.– Cmte Cardeal
Thank you! It worked.
– kastenjhonnes
Good afternoon! Just one more help please. I need the code to put in more than one video on the page as I can change the ID "id="myBtn" to a class?
– kastenjhonnes