在模式弹出窗口中播放嵌入式YouTube视频?

时间:2018-10-31 08:08:51

标签: javascript jquery html

是否可以将YouTube视频嵌入到网站中,并在用户单击时以模式弹出窗口播放?

使用来自YouTube的嵌入代码嵌入视频,这里是一个示例:

<iframe width="560" height="315" src="https://www.youtube.com/embed/iRYDYrj3Bfw" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

我看过几种脚本,它们将YouTube视频加载到模式弹出窗口中,例如:https://appleple.github.io/modal-video/

但是到目前为止,所有人都使用按钮或横幅。

我希望这样做就像这样简单:

<div id="wrapper">
<youtube iframe></iframe>
</div>

然后在javascript中监听#wrapper的点击?

我想效仿以下网站的行为(您可能需要向下滚动到名为“ Arbeiten”的部分,然后单击其中一个视频):http://www.heimat.wien/#arbeiten

2 个答案:

答案 0 :(得分:2)

根据Ingus的解决方案,您可以像在这里一样在iframe上覆盖div:

$(document).ready(function() {
  autoPlayYouTubeModal();
});

function autoPlayYouTubeModal() {
  var trigger = $('.trigger');
  trigger.click(function(e) {
    e.preventDefault();
    var theModal = $(this).data("target");
    var videoSRC = $(this).attr("src");
    var videoSRCauto = videoSRC + "?autoplay=1";
    $(theModal + ' iframe').attr('src', videoSRCauto);
    $(theModal).on('hidden.bs.modal', function(e) {
      $(theModal + ' iframe').attr('src', '');
    });
  });
};
.holder {
  width: 560;
  height: 315px;
  position: relative;
}

.frame {
  width: 100%;
  height: 100%;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 560%;
  height: 315px;
  cursor: pointer;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div class="holder">
  <iframe width="560" height="315" src="https://www.youtube.com/embed/VF1Yz05iQM0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
  <div class="overlay trigger" src="https://www.youtube.com/embed/VF1Yz05iQM0" data-target="#videoModal" data-toggle="modal"></div>
</div>

<div class="modal fade" id="videoModal" tabindex="-1" role="dialog" aria-labelledby="videoModal" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <button type="button" class="close btn-round btn-primary" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
      <div class="embed-responsive embed-responsive-16by9">
        <iframe class="embed-responsive-item" src="" allowfullscreen></iframe>
      </div>
    </div>
  </div>
</div>

正在工作的小提琴:https://jsfiddle.net/nqxeo695/

  

注意:您最好使用自己的图像或视频的屏幕截图,而不要使用iframe,因为每个iframe都会立即加载视频。您在网站上放的视频越多,网站需要加载的时间就越长。

答案 1 :(得分:0)

我将以下示例用于引导程序3/4。它为我工作。

显示视频的youtube缩略图

active

请注意,我的数据库中显示了 $ row ['videoID']

这是将视频保存在iframe上的模式

<img src="https://img.youtube.com/vi/<?= $row['videoID'] ?>/mqdefault.jpg" class="video-btn img-fluid cursor-pointer" data-toggle="modal" data-src="https://www.youtube.com/embed/<?= $row['videoID'] ?>" data-target="#myModal">

和这个简单的脚本

<!-- Modal -->
<div class="modal videomodal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">


      <div class="modal-body">

       <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>        
        <!-- 16:9 aspect ratio -->
<div class="embed-responsive embed-responsive-16by9">
  <iframe class="embed-responsive-item" src="" id="video"  allowscriptaccess="always">></iframe>
</div>

如果要添加一些样式。你可以用这个

<script type="text/javascript">
    //for youtube video modal
$(document).ready(function() {
      var $videoSrc;  
      $('.video-btn').click(function() {
          $videoSrc = $(this).data( "src" );
      });
      console.log($videoSrc);
      $('#myModal').on('shown.bs.modal', function (e) {
      $("#video").attr('src',$videoSrc + "?rel=0&amp;showinfo=0&amp;modestbranding=1&amp;autoplay=1" ); 
      })
      $('#myModal').on('hide.bs.modal', function (e) {
          $("#video").attr('src',$videoSrc); 
      })
});
</script>