I'm very new to javascript and trying to use Twitter bootstrap to get a good looking website up and running quickly. I know this has something to do with jquery, but I'm not sure how to stop my video when I push the close button or the close icon.
Can someone explain how I can get my video to stop playing because even when I close the window, I can still hear it in the background.
<!-- Button to trigger modal --> <a href="#myModal" role="button" class="btn" data-toggle="modal"><img src="img/play.png"></a> <!-- Modal --> <div id="myModal" class="modal hide fade" tabindex="-1" role=labelledby="myModalLabel" aria-hidden="true"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria>×</button> <h3 id="myModalLabel">I am the header</h3> </div> <div class="modal-body"> <p><iframe width="100%" height="315" src="http:com/embed/662KGcqjT5Q" frameborder="0" allowfullscreen></iframe></p> </div> <div class="modal-footer"> <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button> </div> </div>
First, copy the code to embed the video from YouTube site. Then, put the code inside the . modal-body element. To stop video automatically when you close the modal, toggle the URL value of YouTube's video iframe src attribute dynamically using the jQuery.
You can adjust this behaviour by adding in some Bootstrap javascript/jQuery that acts to stop the video if either the modal window is either clicked to close or the background is clicked to dismiss the modal.
When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.
To prevent closing bootstrap modal when click outside of modal window we need add properties data-backdrop="static" and data-keyboard="false" to button which we are using to show modal window like as shown following.
There is a nice proper way of doing this - see the comments in the approved answer to this post.
Couldn't get that working first time round myself though, and was in a rush, so I did a rather horrible hacky bit of code which does the trick.
This snippet 'refreshes' the src of the embed iframe, causing it to reload:
jQuery(".modal-backdrop, #myModal .close, #myModal .btn").live("click", function() { jQuery("#myModal iframe").attr("src", jQuery("#myModal iframe").attr("src")); });
I know I'm 2+ years late, but since then, a couple of things have changed, with B3 the new way to perform this out of the box is this:
$("#myModal").on('hidden.bs.modal', function (e) { $("#myModal iframe").attr("src", $("#myModal iframe").attr("src")); });
Have fun with Bootstrap!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With