Twitter Bootstrap Modal stop Youtube video

后端 未结 21 1651
小蘑菇
小蘑菇 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:20

    This one does it perfectly:

    $("#myModal").on("hidden.bs.modal", function(t) {
       var o = $(t.target).find("iframe");
       o.each(function(t, o) {
          $(o).attr("src", $(o).attr("src"))
       });
    });
    

    If you however want to have it start when modal opens / stop when it closes use this code:

    But make sure to add enablejsapi=1 in your src, like this for example:

    
    
    
    function playStopVideo() {
        var youtubeFunc ='';
        var outerDiv = $("#myModal");
        var youtubeIframe = outerDiv.find("iframe")[0].contentWindow;
        outerDiv.on('hidden.bs.modal', function (e) {
            youtubeFunc = 'stopVideo';
            youtubeIframe.postMessage('{"event":"command","func":"' + youtubeFunc + '","args":""}', '*');
        });
        outerDiv.on('shown.bs.modal', function (e) {
            youtubeFunc = 'playVideo';
            youtubeIframe.postMessage('{"event":"command","func":"' + youtubeFunc + '","args":""}', '*');
        });
    }
    
    playStopVideo();
    

提交回复
热议问题