javascript - 单击模态外时暂停模态中的视频

标签 javascript jquery html twitter-bootstrap

我想在用户单击模态区域之外时暂停视频,目前只有当用户单击关闭按钮时暂停才起作用。我想在视频不在屏幕上时暂停视频。

  autoPlayYouTubeModal();

  //FUNCTION TO GET AND AUTO PLAY YOUTUBE VIDEO FROM DATATAG
  function autoPlayYouTubeModal() {
      var trigger = $("body").find('[data-toggle="modal"]');
      trigger.click(function () {
          var theModal = $(this).data("target"),
              videoSRC = $(this).attr("data-theVideo"),
              videoSRCauto = videoSRC + "?autoplay=1";
          $(theModal + ' iframe').attr('src', videoSRCauto);
          $(theModal + ' button.close').click(function () {
              $(theModal + ' iframe').attr('src', videoSRC);
          });
      });
  }
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>


<a href="#" class="btn btn-default" data-toggle="modal" data-target="#videoModal" data-theVideo="http://techslides.com/demos/sample-videos/small.mp4">VIDEO</a>

<div class="modal fade" id="videoModal" tabindex="-1" role="dialog" aria-labelledby="videoModal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <div>
                    <iframe width="100%" height="350" src=""></iframe>
                </div>
            </div>
        </div>
    </div>
</div>

这是工作示例 fiddle

最佳答案

这个可以尝试

 $('#videoModal').on('hidden.bs.modal', function (e) {
      $('#videoModal .close').trigger('click')
      })

关于javascript - 单击模态外时暂停模态中的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38950300/

相关文章:

javascript - 周 View 中的完整日历更改日期格式

jquery - 如何在最后一个 <div> 上添加 “fit” 类

html - 使用类应用的样式不起作用但样式属性有效

jquery - Bootstrap - div 低于其他,如何带到上面

html - 在 ul 中对齐水平图像和文本链接

javascript - Node.js 忽略除第一个参数之外的所有查询字符串参数

javascript - 如何防止 Flutter Web 中的 CTRL 和滚轮/触控板缩放?

javascript - 使用 Bootstrap 的响应式 3 列布局

javascript - d3.js nvd3 x 轴上的日期 : only some dates are show

jquery - 不要在移动设备中使用 Bootstrap 显示响应段落