我正在尝试使用 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">×</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 代码,但它似乎不起作用。你介意帮助我吗?
非常感谢你,我会感谢我得到的每一个帮助!
蒂娜