What's new
HTML Forums | An HTML and CSS Coding Community

Welcome to HTMLForums; home of web development discussion! Please sign in or register your free account to get involved. Once registered you will be able to connect with other members, send and receive private messages, reply to topics and create your very own. Our registration process is hassle-free and takes no time at all!

Stop Video on Modal Close

r43gf34gsf

New member
I have several modals, each with a different iframe with a different embedded youtube video.

When someone closes out of the modal (either by clicking away, hitting ESC or closing it), I would like the youtube video in the modal to stop.

I am not using bootstrap.

Below is an example of just one of the modals in action.

Any suggestions will help!

Fiddle






HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#" class="button" data-modal="#modal2">Modal2</a>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate.</p>

<div id="modal2" class="modal">
<div class="modal-content">
<a class="close">&times;</a>
<h2>This is an image description 2</h2>
<div class="video-container"><iframe class="youtube-video" width="560" height="315" src="
" frameborder="0" allowfullscreen></iframe>
</div>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate.</p>
</div>
</div>

CSS

/* Modal */
.modal { position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 9999999; display: none; background: rgba(255, 255, 255, 0.5); }
/* Fullscreen BG */
.modal-content h2 {font-size: 1.0em; }
.modal-content { width: 80%; height: 80vh; overflow-x: hidden; overflow-y: auto; margin: 10% auto; font-size: 1.2em; }
.close {line-height: 25px; font-size: 1.0em; position: absolute; right: -24px; text-align: center; top: -6px width: 24px; z-index: 9999999; text-decoration: none; font-weight: bold; cursor: pointer; }
.close:hover { color: grey; }
/* Youtube */
.video-container { overflow: hidden; position: relative; width:100%; }
.video-container::after {padding-top: 56.25%; display: block; content: ''; }
.video-container iframe {position: absolute; top: 0; left: 0; width: 100%; height: 100%; }

JS
//Model
$(".button").on("click", function() {
var modal = $(this).data("modal");
$(modal).fadeIn();
});
//click away to close model
$(".modal").on("click", function(e) {
var className = e.target.className;
if (className === "modal" || className === "close") {
$(this).closest(".modal").fadeOut();
}
});
//ESC close model
$(document).keydown(function(e) {
var code = e.keyCode || e.which;
if (code == 27) $(".modal").fadeOut();
});
 
Back
Top