Twitter Bootstrap Modal stop Youtube video

后端 未结 21 1566
小蘑菇
小蘑菇 2020-11-27 11:57

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

相关标签:
21条回答
  • 2020-11-27 12:01

    I used a combination of Ruslanas Balčiūnas and Nathan McFarland's answers to code something that worked well for me.

    $('#myModal').on('hide',function(){
       $('.modal-body iframe').attr('src','');
    });
    

    So basically this sets the src attribute of the iframe to nothing when the close modal event is triggered. Short and sweet.

    0 讨论(0)
  • 2020-11-27 12:03

    Here is a simple way I've found for having a video play in a modal window, and then stop playing on close.

    Use the .html to load the iFrame with your video into the modal-body, when the modal is shown, and then replace the modal-body with nothing when it is hidden.

    $('#myModal').on('show', function () { 
     $('div.modal-body').html('YouTube iFrame goes here');  
    });
    $('#myModal').on('hide', function () {
     $('div.modal-body').html('');
    });
    

    Here is a jsfiddle example:

    http://jsfiddle.net/WrrM3/87/

    0 讨论(0)
  • 2020-11-27 12:04

    Expanding on Guille's answer above, this is a drop-in function that will work with Bootstrap 3, the latest version of youtube as of Aug 14, and works for multiple videos /modals in one page.

    $(".modal").on('hidden.bs.modal', function(e) {
        $iframe = $(this).find( "iframe" );
        $iframe.attr("src", $iframe.attr("src"));
    }); 
    
    0 讨论(0)
  • 2020-11-27 12:04

    @guillesalazaar's answer was only the 1st half of my fix so I felt compelled to share my situation in case it helps someone in the future. I too have an embedded youtube video but I set mine to play automatically when the frame is loaded using autoplay=1. To get around the autoplay issue on page load I have my youtube url stored in a variable that sets the iframe source using a click handler. To solve this I simply removed the attribute source link:

    My Url to trigger the modal window:

       <div id="movieClick" class="text-center winner"> 
    <a href="#" data-toggle="modal" data-keyboard="true" data-target="#movie">
        </div>
    

    My hidden modal window divs:

    <div id="movie" class="modal fade" role="dialog" tabindex='-1'>
        <div class="modal-dialog">
    
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">They Live (1988)</h4>
        </div>
         <div class="modal-body">
            <iframe id="onscreen" width="100%" height="460px" src="" frameborder="0" allowfullscreen></iframe>
    
          </div>
    </div>
    
    </div>
    </div>
    

    My JS:

    $("#movieClick").click(function(){
    var theLink  = "https://www.youtube.com/embed/Wp_K8prLfso?autoplay=1&rel=0";
    document.getElementById("onscreen").src = theLink;
    });
    
    
    // really annoying to play in the Background...this disables the link
    $("#movie").on('hidden.bs.modal', function (e) {
        $("#movie iframe").attr("src", '');
    });
    
    0 讨论(0)
  • 2020-11-27 12:05

    My solution to this that works for Bootstrap 3 and the modern YouTube embed format is as follows.

    Assuming your video is embedded within a standard Bootstrap 3 Modal with id="#video-modal", this simple bit of Javascript will do the job.

    $(document).ready(function(){
      $("#video-modal").on('hide.bs.modal', function(evt){
        var player = $(evt.target).find('iframe'),
            vidSrc = player.prop('src');
        player.prop('src', ''); // to force it to pause
        player.prop('src', vidSrc);
      });
    });
    

    I've seen proposed solutions to this issue involving use of the YouTube API, but if your site is not an https site, or your video is embedded using the modern format recommended by YouTube, or if you have the no-cookies option set, then those solutions don't work and you get the "TV set to a dead channel" effect instead of your video.

    I've tested the above on every browser I could lay my hands on and it works very reliably.

    0 讨论(0)
  • 2020-11-27 12:07

    1. Embed Youtube on your Page (html)

    Add the Youtube div-container to your Website:

    <div id="ytplayer">This will be replaced with the Youtube iFrame</div>
    

    Add the Youtube Javascript Api (iFrame)

    <script>
        // Ads Youtube JS API
        var tag = document.createElement('script');
        tag.src = "https://www.youtube.com/iframe_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    
    
        // This function creates an <iframe> (and YouTube player) after the API code downloads.
        var player;
        function onYouTubeIframeAPIReady() {
        player = new YT.Player('ytplayer', {
          height: '390',
          width: '640',
          videoId: 'ncif63mSZl4',
            WMode: 'transparent',
            wmode: 'opaque',
            playerVars: {
                'autoplay': 0,
                'controls': 0,
                'autohide':1,
                'rel':0,
                'showinfo': 0,
                'modestbranding': 1,
            },
          });
        }
    
       function stopVideo() {
        player.stopVideo();
       }
        
       function pauseVideo() {
        player.pauseVideo();
       }
        
       function playVideo(){
        player.playVideo();
       }
    </script>
    

    Controll with jQuery (Bootstrap Modal)

    
      // Open Modal and start Playing Youtube Video
      $("#myModalTrigger").click(function(){
        $("#video-modal").modal(); // opens up the Modal
        playVideo(); // Starts playing Video
      });
    
      // Stop Player on Modal hide
      $('#video-modal').on('hide.bs.modal', function (e) {
        stopVideo(); // stop player
      });
    
    
    0 讨论(0)
提交回复
热议问题