问题描述
我整理了以下代码,它们可以执行以下操作: 在页面上显示图像。 单击图像后,将打开一个包含可播放视频的模态。 当用户在模态外部单击时,关闭模态。 当模式关闭时,停止播放视频。 但是视频不会停止播放。 有什么帮助吗?我是编码新手。
<!--Modal video-->
<img src="img/myimg.png" data-toggle="modal" data-target="#videoModal" />
<div class="modal fade" id="videoModal" tabindex="-1" role="dialog" aria-labelledby="videoModal" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body p-0">
<video controls width="100%" preload="auto" class="embed-responsive embed-responsive-1by1">
<source src="vids/myvideo.mp4" type="video/mp4">
</video>
</div>
</div>
</div>
</div>
解决方法
您可以在.pause()上hide.bs.modal播放视频(即:关闭模式后):
$('#videoModal').on('hide.bs.modal',function(e) {
this.querySelector('video').pause();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<img src="img/myimg.png" data-toggle="modal" data-target="#videoModal" />
<div class="modal fade" id="videoModal" tabindex="-1" role="dialog" aria-labelledby="videoModal" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body p-0">
<video controls width="100%" preload="auto" class="embed-responsive embed-responsive-1by1">
<source src="https://www.w3schools.com/tags/movie.mp4" type="video/mp4">
</video>
</div>
</div>
</div>
</div>
,
这是我处理在模态中加载的任何类型的内容的方式。
点击模式外的关闭视频(或内容) - BOOTSTRAP 4
$(document).click(function(event) {
if (!$(event.target).is("#YOUR-MODAL-ID")) {
alert('I just clicked inside the modal');
//does not pop up on embeded elements
}
else {
--> Basically anything you want to stop playing when clicking outside the modal goes here
--> be sure to call the right statement for whatever is inside the modal
like:
- var oldPlayer = document.getElementById('ID-OF-PLAYER-WRAPPER');
- videojs(oldPlayer).dispose();
- $("#wrapper-div-id iframe").attr("src",'');
}
});