I need auto-play of youtube videos when the Twitter Bootstrap motive was opened and subsequently stopped the video from closing.
I know this question has been asked before, but the answers I can find will lead to a lot of javascript code for a page containing a lot of videos, and I'm trying to reduce the swelling. So far I have been working on individual videos, but the problem is that every modal and every video should have this javascript code repeated with the corresponding variables.
$('#vidThumbnail-1').click(function () { var src = 'http://www.youtube.com/embed/8bKmrhI-YT8?&autoplay=1'; $('#vid1').modal('show'); $('#vid1 iframe').attr('src', src); //Fit Vids Implamented Below for Mobile Compatibility $("#vid1Thumbnail-1 iframe").wrap("<div class='flex-video'/>"); $(".flex-video").fitVids(); }); $('#vid1 button').click(function () { $('#vid1 iframe').removeAttr('src'); });
HTML:
<div id="vidThumbnail-1"><img src="http://img.youtube.com/vi/8bKmrhI-YT8/0.jpg" /></div> <div id="vid1" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-body"> <iframe src="http://www.youtube.com/embed/8bKmrhI-YT8" width="640" height="360" frameborder="0" allowfullscreen></iframe> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> </div> </div>
This is inflated when you have 20 videos and 20 modals! Is there a way to use the Data Attributes method to launch a modal built-in bootstrap, and not to call it individually, but at the same time modify the iframe src only for the target modal? Then the link to open the modal will be as follows:
<a href="#vid1" role="button" data-toggle="modal"> <img src="http://img.youtube.com/vi/8bKmrhI-YT8/0.jpg"></a>
Then:
- Read the existing src iframe attribute in the target modal (set it to a variable?)
- To initiate the video, add the existing src attribute with the parameter "& autoplay = 1".
- Replace the src attribute with the original when closing the modal (using the button, in particular, as shown above).
This way, I would not need to write a script for each individual modal, saving a lot of time and bloated JS. However, I do not know where to start modifying bootstrap.js to accomplish this, and I do not experience JS (or any) programming. Thanks for any help you can give me!
source share