模态关闭时停止播放视频

cpa*_*mer 0 html javascript jquery modal-dialog twitter-bootstrap

我正在使用以模式打开的HTML中的视频标签。目前,如果我退出模式而不暂停视频,它仍在播放。我还没有JavaScript,因为我添加的所有内容均无法正常运行。我也在使用引导程序。这是我的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>
Run Code Online (Sandbox Code Playgroud)

Bob*_*Bob 6

我希望这个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>
Run Code Online (Sandbox Code Playgroud)

JS:

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


Gor*_*ram 5

使用hidden.bs.modal事件。

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

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


Szu*_*u.W 5

无需添加任何 JS,您只需编写一个简单的 onclick 操作即可在用户关闭模式时暂停您的视频。

onclick="document.getElementById('demoVideo').pause();"
Run Code Online (Sandbox Code Playgroud)

记得改成'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>
Run Code Online (Sandbox Code Playgroud)

*记得将 更改src为您的视频。