html Bootstrap Youtube Modal

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html Bootstrap Youtube Modal相关的知识,希望对你有一定的参考价值。

  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);
          });
      });
      
      //This code triggers the close class on clicking outside Modal
      $('#videoModal').on('hidden.bs.modal', function (e) {
      $('#videoModal .close').trigger('click')
      })
  }
<a href="#" class="btn btn-default" data-toggle="modal" data-target="#videoModal" data-theVideo="https://www.youtube.com/embed/loFtozxZG0s">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>

以上是关于html Bootstrap Youtube Modal的主要内容,如果未能解决你的问题,请参考以下文章

html 完整示例 - 在模态中自动播放youtube视频(Bootstrap + Jquery)

我如何在 twitter bootstrap 3 中将 Youtube Video (iframe) 居中?

Bootstrap 轮播 - 播放 YouTube 视频时暂停

javascript YouTube Bootstrap Modal AutoPlay

将 Youtube 视频正确嵌入到 bootstrap 3.0 页面

Bootstrap 模式关闭时 Youtube 视频仍在播放