模态关闭时停止播放视频

时间:2015-11-02 09:40:55

标签: 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 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">&times;</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>

7 个答案:

答案 0 :(得分:4)

使用hidden.bs.modal事件。

  

当模态完成对用户的隐藏时将触发此事件(将等待CSS转换完成)​​。

$(function(){
    $('#myModal').modal({
        show: false
    }).on('hidden.bs.modal', function(){
        $(this).find('video')[0].pause();
    });
});

答案 1 :(得分:2)

如果不添加任何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更改为您的视频。

答案 2 :(得分:1)

我希望Plunker可以帮到你,我也用同样的方法来解决问题。

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">&times;</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">&times;</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:

$(function(){
  $('.modal').on('hidden.bs.modal', function (e) {
    $iframe = $(this).find("iframe");
    $iframe.attr("src", $iframe.attr("src"));
  });
});

答案 3 :(得分:1)

如果您有动态加载的无限模态,请使用以下方法:

(function ($) {
  $(document).on('hidden.bs.modal', function (e) {
    var video = document.getElementById('player-' + e.target.id);
    video.pause();
    video.currentTime = 0;
  });
})(jQuery);

答案 4 :(得分:0)

谢谢Pinegrow - 这对我有用:

$('.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);
});

答案 5 :(得分:0)

首先向您的视频播放器添加一个ID,例如

<video width="700" height="400" controls="controls" preload="auto" autoplay="true" id="myPlayer" name="myPlayer">
//codes..
</video>

然后添加以下代码以关闭模态事件

document.getElementById("myPlayer").pause();
        document.getElementById("myPlayer").currentTime = 0;

答案 6 :(得分:0)

我们如何在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
        
});