我在SO的不同帖子上尝试了解决方案,但似乎都无法正常工作。
我有一个带有播放视频的iframe的模态,这似乎可行,但是当我关闭模态时,我一直试图完全停止视频,但它似乎仍然尝试播放。
iframe元素
<iframe class="modal-iframe" type="text/html" width="640" height="360" src="https://www.youtube.com/embed/VIDEO?&controls=0&rel=0&showinfo=0&playlist=VIDEO&loop=1&autoplay=1" frameborder="0"></iframe>
开幕/闭幕活动
$(".custom-modal").on("hidden.bs.modal", function (e) {
var iframe = $('#' + e.target.id).find("iframe");
if (iframe.attr("src") && iframe.attr('src').indexOf('autoplay') != -1) {
var newURL = iframe.attr('src').substring(0, iframe.attr('src').length - 11);
iframe.attr('src', newURL);
}
})
$('.custom-modal').on('show.bs.modal', function (e) {
var iframe = $('#' + e.target.id).find("iframe");
if (iframe) {
var videoSrc = iframe.attr("src");
iframe.attr("src", videoSrc+"&autoplay=1");
}
});
这似乎在某种程度上可以正常工作,并且autoplay
标签已正确添加/删除,但是由于某种原因,当模式关闭时,由于控制台出现错误,视频似乎仍然可以尝试再次播放(实际上并不能播放,但是当我有多个使用不同模式的视频时,我可以看到这是一个问题)
So the autoplay tag is removed when the modal is closed, but this thread still seems to exist for the video.
How can I stop the video completely so it doesn't send any more requests when the modal is closed?
So I think I've found the problem. The iframe is loaded onto the main page when the modal is opened, and it is always present in the background. I have tried removing the iframe via remove()
and this works and stops the errors but the iframe then does not load a second time when the modal is re-opened.
I need a solution to unload the iframe, but still allow it to be reloaded when the modal is opened.
I just created two video urls, one with autoplay and one without and then just toggled between two and it is working for me :
var video = "https://www.youtube.com/embed/ogfYd705cRs";
var autoVideo = video + "?modestbranding=1&rel=0&controls=0&showinfo=0&html5=1&autoplay=1";
$(".custom-modal").on("hidden.bs.modal", function (e) {
var iframe = $('#' + e.target.id).find("iframe");
iframe.attr("src", video);
})
$(".custom-modal").on("show.bs.modal", function (e) {
var iframe = $('#' + e.target.id).find("iframe");
iframe.attr("src", autoVideo);
});
So why not storing video URL in a data attribute ?
<iframe class="modal-iframe" type="text/html" width="640" height="360" src="" data-video="https://www.youtube.com/embed/ogfYd705cRs" frameborder="0"></iframe>
然后在函数内部获取视频URL:
$(".custom-modal").on("hidden.bs.modal", function (e) {
var iframe = $('#' + e.target.id).find("iframe");
var video = iframe.attr("data-video");
iframe.attr("src", video);
})
$('.custom-modal').on('show.bs.modal', function (e) {
var iframe = $('#' + e.target.id).find("iframe");
var autoVideo = iframe.attr("data-video") + "?modestbranding=1&rel=0&controls=0&showinfo=0&html5=1&autoplay=1";
iframe.attr("src", autoVideo);
});
对于每个模式的多个视频,将视频网址放在用于打开模式的锚点上:
<a href="modals/somemodal.html" class="video-anchor" data-target="#modal1" data-video="https://www.youtube.com/embed/ogfYd705cRs" data-toggle="modal">
而是监听模式打开,监听锚点单击和:
$(".video-anchor").click(function () {
var modal = $(this).data("target");
var video = $(this).attr("data-video");
var autoVideo = video + "?modestbranding=1&rel=0&controls=0&showinfo=0&html5=1&autoplay=1";
$(modal + ' iframe').attr('src', autoVideo);
});
当模式关闭时,设置为src并不重要,因此:
$(".custom-modal").on("hidden.bs.modal", function (e) {
var iframe = $('#' + e.target.id).find("iframe");
iframe.attr("src", "");
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句