0

我正在尝试使用 Bootstrap 的 Modal 在我正在开发的网站上播放 youtube 视频,但我遇到了一个问题。我从这里阅读提示:Bootstrap 3 and youtube in Modal

唯一对我来说效果很好的是:

杰里米肯尼迪小提琴

但问题是,当我在播放视频时单击模态框外部将其关闭时,视频不会停止,所以我仍然可以在后台听到它。我尝试搜索答案:google、youtube、stackoverflow,但没有任何效果。

我一直在阅读以使用 hidden.bs.modal。我已经搜索和搜索并玩过它,但它仍然在后台播放。这是我的代码:

HTML:

<a href="#" class="btn btn-default" data-toggle="modal" data-target="#videoModal" data-theVideo="http://www.youtube.com/embed/loFtozxZG0s">VIDEO</a>
<div class="modal fade" id="videoModal" tabindex="-1" role="dialog" aria-labelledby="videoModal" aria-hidden="true">
<div class="modal-dialog">
 <div class="modal-content">
<div class="modal-body">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <div>
                <iframe width="100%" height="350" src=""></iframe>
            </div>
        </div>
    </div>
</div>

JS文件:

  autoPlayYouTubeModal();
    //FUNCTION TO GET AND AUTO PLAY YOUTUBE VIDEO FROM DATATAG
     function autoPlayYouTubeModal() {
       var trigger = $("body").find('[data-toggle="modal"]');
trigger.click(function () {
      var theModal = $(this).data("target"),
          videoSRC = $(this).attr("data-theVideo"),
          videoSRCauto = videoSRC + "?autoplay=1";
      $(theModal + ' iframe').attr('src', videoSRCauto);
      $(theModal + ' button.close').click(function () {
          $(theModal + ' iframe').attr('src', videoSRC);
      });
    $("#videoModal").on('hidden.bs.modal', function (e) {
    $("#videoModal iframe").attr("src", $("#videoModal iframe").attr("src"));
});
  });}

我添加了在该页面上的一个答案中找到的 hidden.bs.modal 代码,但它似乎不起作用。你介意帮助我吗?

非常感谢你,我会感谢我得到的每一个帮助!

蒂娜

4

2 回答 2

1

看看这个小提琴

我使用了 YouTube IFrame API,只需几行代码即可轻松启动和停止视频。小提琴当前在模态打开后启动视频,并在模态关闭后立即停止。

var player;

function loadVideo() {
    player = new YT.Player('player', {
        width: '100%',
        videoId: 'loFtozxZG0s',
    });
}

$('#myModal').on('hide.bs.modal', function (e) {
    player.stopVideo();
});

$('#myModal').on('shown.bs.modal', function (e) {
    player.playVideo();
});

loadVideo 函数在#playerdiv 中创建一个新的 YouTube 播放器。这是设置视频的地方,videoId.

hide.bs.modal下一个功能会在模态开始关闭时立即停止触发的视频。

最后一个函数启动在shown.bs.modal模态显示后触发的视频。

您可以在此处找到有关 YouTube IFrame API 的更多信息。

希望这行得通,

图格兹里达。

于 2015-05-25T10:42:34.540 回答
0

这对我有用:

<script>
    //function to get and autoplay youtube video from datatag:
   function autoPlayYouTubeModal(){
        var trigger = $("body").find('[data-toggle="modal"]');
        trigger.click(function() {
            var theModal = $(this).data( "target" ),
                    videoSRC = $(this).attr( "data-theVideo" ),
                    videoSRCauto = videoSRC+"?rel=0&amp;autoplay=1" ;
            $(theModal+' iframe').attr('src', videoSRCauto);
            $(theModal+' button.close').click(function () {
                $(theModal+' iframe').attr('src', videoSRC);
            });
            $(theModal).on('hide.bs.modal', function (e) {
    $(theModal+' iframe').attr('src', videoSRC);
});
        });
    }
    //the function call:
            $(document).ready(function(){
                autoPlayYouTubeModal();
            });
            </script>

我补充说:

$(theModal).on('hide.bs.modal', function (e) {
    $(theModal+' iframe').attr('src', videoSRC);
});
于 2016-10-12T12:47:49.773 回答