【发布时间】:2017-01-04 16:45:49
【问题描述】:
我在同一页面上有两个视频,它们在 iframe 中打开。当我关闭弹出窗口时,视频不会停止。它一直在播放。由于我无法控制的情况,我必须使用 iframe 中的视频。
谁能帮忙,下面是相同的代码:
jQuery:
$("[data-media]").on("click", function(e) {
e.preventDefault();
var $this = $(this);
var videoUrl = $this.attr("data-media");
var popup = $this.attr("href");
var $popupIframe = $(popup).find("iframe");
$popupIframe.attr("src", videoUrl);
var left = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var left = left/2 - 340;
var top = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
var top = top/2 - 180;
document.getElementById("vid").style.top = top + "px";
document.getElementById("vid").style.left = left + "px";
document.getElementById("vid1").style.top = top + "px";
document.getElementById("vid1").style.left = left + "px";
$this.closest(".page").addClass("show-popup");
});
$(".popup").on("click", function(e) {
e.preventDefault();
e.stopPropagation();
$(".page").removeClass("show-popup");
});
$(".popup > iframe").on("click", function(e) {
e.stopPropagation();
});
HTML:
<div class="popup" id="media-popup"> <!-- video embedded -->
<iframe id="vid" src="http://player.vimeo.com/video/1212121210?title=0&byline=0&portrait=0" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<iframe class="show-2" style="display: none;" id="vid1" src="http://player.vimeo.com/video/112324343?title=0&byline=0&portrait=0" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<a class="video-close" href="#0"></a>
</div><!-- popup -->
<a id="video" data-media="//www.vimeo.com/134243242">video 1</a>
<a id="video" class="video-2" data-media="//www.vimeo.com/00102102">Video 2</a>
【问题讨论】:
-
如果可能的话,我还建议您使用
video元素,而不是直接将iframe的src设置为视频,因为它为您提供了更多控制权 - 您将获得@987654327 @ 方法之一。 -
@RoryMcCrossan 你能分享一个例子吗?这可能对我有帮助。
-
您的弹出窗口只是页面中的一个 html 元素。以弹出窗口的形式显示或隐藏。但是隐藏它并不会从页面中删除元素,因此即使您看不到它,视频仍然会播放。您需要重置 iframe 的 url(为什么要为此使用 iframe?)
-
@Seb,我不得不使用它,因为另一个编码器开发了这部分代码及其庞大的数据来改变一切
标签: javascript jquery