我正在尝试停止播放模式中的视频。问题是我的模态脚本将模态从原始位置移动到结束</body>
标记之前。因此,在技术上高于模态窗口时停止视频脚本,视频在模态关闭后永远不会停止播放。
这是我使用https://github.com/VodkaBears/Remodal
的模态脚本JQUERY停止视频
var stopVideo = function ( element ) {
var video = element.querySelector( 'video' ); // script stops here with this error message: (index):684 Uncaught TypeError: Cannot read property 'querySelector' of null.
if ( video !== null ) {
video.stop();
}
};
$('.remodal-close').click(function(){
var id = this.id || this.getAttribute( 'data-remodal-id' );
var modal = document.querySelector( id );
//closePopup();
console.log("has video stopped? 1");
stopVideo( modal );
console.log("has video stopped? 2");
});
HTML FOR MODAL
<div class="remodal" data-remodal-id="modal" role="dialog" aria-labelledby="modal1Title" aria-describedby="modal1Desc">
<button data-remodal-action="close" class="remodal-close" aria-label="Close"></button>
<div class="video-container clearfix">
<div class="video clearfix">
<embed width="200" height="113" src="https://www.youtube.com/embed/xxxxxxxx?autoplay=1" frameborder="0" allowfullscreen>
</div>
</div>
</div>
答案 0 :(得分:1)
点击Video stop button
时触发点击modal-close-button
即可停止此操作。这只是一个例子,因此必要时进行调整。
$("#modal-close-button").click(function () {
$("#video-stop-button").click();
});
$("#video-stop-button").click(function () {
alert("The video should stop as the modal closes because a click on the close button will trigger the stop button ");
});
&#13;
div {
cursor: pointer;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="modal-close-button"> Modal close button</div>
<div id="video-stop-button"></div>
&#13;
答案 1 :(得分:0)
我的猜测是,您在停止视频方面遇到了麻烦,因为它在iframe中运行。您可以尝试这样做(请注意,我使用iframe代码而不是embed代码):
function toggleVideo(e, state) {
var div = $(e.target)[0];
var iframe = div.getElementsByTagName("iframe")[0].contentWindow;
div.style.display = state == 'hide' ? 'none' : '';
func = state == 'hide' ? 'pauseVideo' : 'playVideo';
iframe.postMessage('{"event":"command","func":"' + func + '","args":""}', '*');
}
$(document).on('opening', '.remodal', function(e) {
toggleVideo(e);
});
$(document).on('closing', '.remodal', function(e) {
toggleVideo(e, 'hide');
});
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/remodal/1.0.7/remodal.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/remodal/1.0.7/remodal-default-theme.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/remodal/1.0.7/remodal.min.js"></script>
<a href="#modal">Call the modal with data-remodal-id="modal"</a>
<div class="remodal" data-remodal-id="modal" role="dialog" aria-labelledby="modal1Title" aria-describedby="modal1Desc">
<button data-remodal-action="close" class="remodal-close" aria-label="Close"></button>
<div class="video-container clearfix">
<div class="video clearfix">
<iframe width="500" height="315" src="http://www.youtube.com/embed/i1EG-MKy4so?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
</div>
</div>
</div>
&#13;
我的答案受到Rob W
的回答的启发您可以在Github Page
上找到有关remodal.js事件的更多信息