Javascript Modal关闭时停止播放视频
我正在使用HTML中的视频标记,它以模式打开。当前,如果我退出模式而不暂停视频,它仍在播放。我还没有javascript,因为我添加的所有东西都不起作用。我也在使用bootstrap。这是我的HTML:Javascript Modal关闭时停止播放视频,javascript,jquery,html,twitter-bootstrap,modal-dialog,Javascript,Jquery,Html,Twitter Bootstrap,Modal Dialog,我正在使用HTML中的视频标记,它以模式打开。当前,如果我退出模式而不暂停视频,它仍在播放。我还没有javascript,因为我添加的所有东西都不起作用。我也在使用bootstrap。这是我的HTML: <button type="button" data-toggle="modal" data-target="#myModal"> <h4>SHORT SLEEVED SHIRT<br><br>$20</h4> <img
<button type="button" data-toggle="modal" data-target="#myModal">
<h4>SHORT SLEEVED SHIRT<br><br>$20</h4>
<img src="images/femaleshortsleeved.jpg"> </button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<video class="video" width="960px" controls>
<source src="images/Short Sleeved Shirt.mp4" type="video/mp4">
</video>
<h2>Short Sleeved Shirt<br>$20</h2>
<h5>90s lightweight brown patterned shirt.<br>No marked size.<br>Will fit S to M.<br>Length: 62cm<br>Width: 56cm</h5>
<button type="button" class="btn btn-primary btn-lg">BUY NOW</button>
</div>
</div>
</div>
短袖衬衫
BR> 20美元
&时代;
短袖衬衫(BR> 20美元)
90年代轻质棕色图案衬衫。
无标记尺码。
适合S至M。
长度:62厘米
宽度:56厘米
立即购买
使用事件
当模式对用户完成隐藏时(将等待CSS转换完成),将触发此事件
谢谢-这对我很有用:
$('.videoModal').on('hide.bs.modal', function(e) {
var $if = $(e.delegateTarget).find('iframe');
var src = $if.attr("src");
$if.attr("src", '/empty.html');
$if.attr("src", src);
});
我希望这能帮助你,我用同样的方法得到了解决方案
HTML:
关闭模式下的自动停止视频
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
<link rel="stylesheet" href="style.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>
<script src="script.js"></script>
</head>
<body>
<h1>Autostop Videos in Closed Modal</h1>
<ul class="nav" >
<li><a href="#" data-toggle="modal" data-target="#video1">Video 1</a></li>
<li><a href="#" data-toggle="modal" data-target="#video2">Video 2</a></li>
</ul>
<div class="modal fade" id="video1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" >
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Video 1</h4>
</div>
<div class="modal-body">
<iframe src="//player.vimeo.com/video/108792063" width="500" height="300" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
</div>
</div>
</div>
<div class="modal fade" id="video2">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" >
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Video 2</h4>
</div>
<div class="modal-body">
<iframe src="//player.vimeo.com/video/69593757" width="500" height="300" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
</div>
</div>
</div>
</body>
</html>
不添加任何JS,您只需编写一个简单的onclick操作,在用户关闭模式时暂停视频
onclick="document.getElementById('demoVideo').pause();"
请记住将'demoVideo'
更改为您自己的#VideoID
完整代码:
<a href="#myVideo"data-toggle="modal" onclick="document.getElementById('demoVideo').play();">--> Watch Video</a>
<!-- Modal -->
<div id="myVideo" class="modal fade" style="display: none;" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"
onclick="document.getElementById('demoVideo').pause();">×</button>
<h4 class="modal-title">Demo Video</h4>
</div>
<div class="modal-body">
<video id="demoVideo" width="560" height="315" controls >
<source src="images/mydemovideo.mp4" type="video/mp4">
</video>
</div>
</div>
</div>
</div>
×
演示视频
*请记住将
src
更改为您的视频。首先向视频播放器添加一个id,如
<video width="700" height="400" controls="controls" preload="auto" autoplay="true" id="myPlayer" name="myPlayer">
//codes..
</video>
如何在modalpopup中暂停/停止YouTube iframe视频
$('#close_one').click(function (e) {
let link = document.querySelector('.divclass');// get iframe class
let link = document.querySelector('#divid');// get iframe id
let video_src = link.getAttribute('src');
$('.youtube-video').children('iframe').attr('src', ''); // set iframe parent div value null
$('.youtube-video').children('iframe').attr('src', video_src);// set iframe src again it works perfect
});
如果动态加载了无限模态,请使用以下方法:
(function ($) {
$(document).on('hidden.bs.modal', function (e) {
var video = document.getElementById('player-' + e.target.id);
video.pause();
video.currentTime = 0;
});
})(jQuery);
你有没有试过在你的电脑上使用.pause()函数?你来的真像上帝,谢谢你…你刚刚救了我…成功了
document.getElementById("myPlayer").pause();
document.getElementById("myPlayer").currentTime = 0;
$('#close_one').click(function (e) {
let link = document.querySelector('.divclass');// get iframe class
let link = document.querySelector('#divid');// get iframe id
let video_src = link.getAttribute('src');
$('.youtube-video').children('iframe').attr('src', ''); // set iframe parent div value null
$('.youtube-video').children('iframe').attr('src', video_src);// set iframe src again it works perfect
});
(function ($) {
$(document).on('hidden.bs.modal', function (e) {
var video = document.getElementById('player-' + e.target.id);
video.pause();
video.currentTime = 0;
});
})(jQuery);