【问题标题】:Play embedded YouTube video in modal popup?在模式弹出窗口中播放嵌入的 YouTube 视频?
【发布时间】:2018-10-31 08:08:50
【问题描述】:

是否可以让嵌入网站的 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

【问题讨论】:

  • 谢谢。但在这个例子中,他们也在使用页面上的按钮来启动模式。想知道这是否可以通过已经嵌入的 youtube 视频来完成? :-)
  • 如果我理解你是正确的,那么你应该用你想要的任何东西替换按钮(例如 div)
  • 视频是使用来自 youtube 的 iframe 嵌入的。当有人点击播放按钮时,我希望视频在弹出窗口中播放。

标签: javascript jquery html


【解决方案1】:

根据 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 都会立即加载视频。您在网站上放置的视频越多,您的网站需要加载的时间就越长。

【讨论】:

  • 感谢您,与此同时,我整理了自己的解决方案,这些解决方案是从网上找到的各种 sn-ps 中复制而来的。在您的情况下,在我的情况下,视频会自动播放声音,除非我将“&mute = 1”附加到网址,否则我无法让它自动播放。我不明白为什么。 codepen.io/CodeChaos/pen/mzZGeK
【解决方案2】:

我将以下示例用于引导程序 3/4。它对我有用。

显示视频的 youtube 缩略图

<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">

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

这是在 iframe 上保存视频的模态

<!-- 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>

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

.videomodal .modal-dialog {
      max-width: 800px;
      margin: 30px auto;
  }    
.videomodal .modal-body {
  position:relative;
  padding:0px;
}
.videomodal .close {
  position:absolute;
  right:-30px;
  top:0;
  z-index:999;
  font-size:2rem;
  font-weight: normal;
  color:#fff;
  opacity:1;
}
.cursor-pointer{
    cursor: pointer;
}

【讨论】:

    猜你喜欢
    • 2016-02-02
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 2012-08-12
    • 2011-02-19
    • 2014-05-27
    相关资源
    最近更新 更多