【问题标题】:Bootstrap 3 and Youtube in Modal模态中的 Bootstrap 3 和 Youtube
【发布时间】:2013-09-08 11:19:59
【问题描述】:

我正在尝试使用 Bootstrap 3 中的模态功能来显示我的 Youtube 视频。它有效,但我无法点击 Youtube 视频中的任何按钮。

有什么帮助吗?

这是我的代码:

<div id="link">My video</div>

<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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">×</button>
            </div>
            <div class="modal-body">
                <iframe width="400" height="300" frameborder="0" allowfullscreen=""></iframe>
            </div>
        </div>
    </div>
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/jquery-1.10.1.min.js"><\/script>')</script>
<script src="js/bootstrap.min.js"></script>
<script>
    $('#link').click(function () {
        var src = 'http://www.youtube.com/v/FSi2fJALDyQ&amp;autoplay=1';
        $('#myModal').modal('show');
        $('#myModal iframe').attr('src', src);
    });

    $('#myModal button').click(function () {
        $('#myModal iframe').removeAttr('src');
    });
</script>

【问题讨论】:

    标签: jquery css twitter-bootstrap youtube twitter-bootstrap-3


    【解决方案1】:

    对于引导程序 5

    在这里我分享对我有用的东西

    触发按钮

    <button data-bs-toggle="modal" data-tagVideo="https://www.youtube.com/embed/zcX7OJ-L8XQ" data-bs-target="#videoModal">Video</button>
    

    模态语法

    <div class="modal fade" id="videoModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="videoModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
            <div class="ratio ratio-16x9">
              <iframe src="" allow="autoplay;" allowfullscreen></iframe>
            </div>
          </div>
        </div>
      </div>
    </div>
    

    从数据中获取和自动播放视频的功能

    function autoPlayYouTubeModal() {
      var triggerOpen = $("body").find('[data-tagVideo]');
      triggerOpen.click(function() {
        var theModal = $(this).data("bs-target"),
          videoSRC = $(this).attr("data-tagVideo"),
          videoSRCauto = videoSRC + "?autoplay=1";
        $(theModal + ' iframe').attr('src', videoSRCauto);
        $(theModal + ' button.btn-close').click(function() {
          $(theModal + ' iframe').attr('src', videoSRC);
        });
      });
    }
    

    在文档就绪时调用函数

    <script>
      $(document).ready(function() {
        autoPlayYouTubeModal();
      });
    </script>
    

    这里的 jsfiddle 可能在第一次运行时有一些 Cors 策略,所以只需刷新并再次单击按钮

    【讨论】:

    • 谢谢@Alemens!使用此 b5 代码单击外部后如何停止视频运行?如果要删除 data-bs-backdrop="static" data-bs-keyboard="false"。
    • 嗨@EvgenySudakov,如果您想在点击外部后停止运行视频,请参阅Bootstrap 5 Options 让您可以设置data-bs-backdrop="true"data-bs-keyboard="true",因此您还可以在按下转义键时关闭画布外
    【解决方案2】:

    引导程序 5 (for readers reaching this from the Bootstrap docs)

    这是一个旧的 Bootstrap 3 问题,但我还没有找到 任何 解决控制播放问题的答案。

    如 Bootstrap 文档中所述,引用该问题是为了解决“模态需要额外的 JavaScript 不在 Bootstrap 中自动停止播放等等”的问题

    只要您使用 iframe 而不是 video 元素来防止 CORS 错误,YouTube 视频就会在 Bootstrap 模式中正常工作。 但是iframe 没有像 autoplay, loop, etc... 这样的视频特定属性,这意味着唯一的播放控件是嵌入在 YT 视频中的控件。例如,当模式打开时,您不能 autostart 视频(autostart=1 不再适用于 YT API)

    controlling YouTube video playback is explained here 的新方法。以下是它在 Bootstrap 5 模态中特别适用于视频播放的方式。

    模态标记

    <div class="modal fade" id="dynamicVideoModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-fullscreen">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title"></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body p-0">
                    <div class="ratio ratio-21x9" id="player">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn-dark" id="playBtn">Play</button>
                    <button type="button" class="btn-dark" id="pauseBtn">Pause</button>
                </div>
            </div>
        </div>
    </div>
    

    JavaScript

    var player
    function onYouTubeIframeAPIReady() {
        console.log('onYouTubeIframeAPIReady...')
        player = new YT.Player('player', {
            videoId: 'M7lc1UVf-VE', // YT video source
            playerVars: {
                'playsinline': 1
            },
            events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
            }
        })
    }
    
    function onPlayerReady(event) {
        event.target.playVideo() // autostart
    }
    
    function onPlayerStateChange(event) {
        // do other custom stuff here by watching the YT.PlayerState
    }
    
    function loadYouTubeVideo() {
        // 2. This code loads the IFrame Player API code asynchronously.
        var tag = document.createElement('script');
        tag.src = "https://www.youtube.com/iframe_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    
    }
    
    var myModalEl = document.getElementById('dynamicVideoModal')
    myModalEl.addEventListener('show.bs.modal', function (event) {
        // dynamically create video when modal is opened
        loadYouTubeVideo()
    })
    
    
    // optional hooks for controls outside YT
    var playBtn = document.getElementById('playBtn')
    playBtn.addEventListener('click', function (event) {
        console.log('play')
        player.playVideo()
    })
    
    var pauseBtn = document.getElementById('pauseBtn')
    pauseBtn.addEventListener('click', function (event) {
        console.log('pause')
        player.pauseVideo()
    })
    

    Bootstrap 5 YouTube Playback Demo

    【讨论】:

    • 感谢 VanillaJS Bootstrap5 回答 Zim。要添加的几点说明:(1)。上线:player = new YT.Player('player', { ...括号中的'player'对应于模态体内的div#player。 (2)。 YT iframe API 脚本的加载是异步的,因此onPlayerReady(event) 似乎是触发视频播放的唯一方法。换句话说,即使loadYouTubeVideo()show.bs.modal 事件中,你也不能在shown.bs.modal 事件中运行player.playVideo(),因为它可能不会及时准备好!
    • (3)。函数onYouTubeIframeAPIReady() 必须在全局范围内,例如,我无法将它包装在我的自定义文档就绪处理程序中。 (4)。由于 Bootstrap 的响应特性,模态框在不同断点处的宽度有不同的设置,因此您可能需要实现 this technique to make the video fluid width inside .modal-body
    • (5)。如果您想使用模态作为模板来启动用户可能点击的不同视频,那么在#player 中我建议使用<iframe> technique here。然后您可以在调用loadYouTubeVideo()之前生成一个新的src字符串并更改show.bs.modal上的iframe源
    • 酷,感谢您的反馈。我会更新答案。
    • 干杯,不确定您的答案是否需要更新!我只是说我是在为其他人添加一些关于我遇到的事情的注释,但实现方式略有不同:)
    【解决方案3】:

    试试这个Bootstrap 4

    <!DOCTYPE html>
    <html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    </head>
    <body>
    
    <div class="container-fluid">
    <h2>Embedding YouTube Videos</h2>
    <p>Embedding YouTube videos in modals requires additional JavaScript/jQuery:</p>
    
    <!-- Buttons -->
    <div class="btn-group">
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#videoModal" data-video="https://www.youtube.com/embed/lQAUq_zs-XU">Launch Video 1</button>
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#videoModal" data-video="https://www.youtube.com/embed/pvODsb_-mls">Launch Video 2</button>
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#videoModal" data-video="https://www.youtube.com/embed/4m3dymGEN5E">Launch Video 3</button>
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#videoModal" data-video="https://www.youtube.com/embed/uyw0VZsO3I0">Launch Video 4</button>
    </div>
    
    <!-- Modal -->
    <div class="modal fade" id="videoModal" tabindex="-1" role="dialog">
        <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header bg-dark border-dark">
                    <button type="button" class="close text-white" data-dismiss="modal">&times;</button>
                </div>
                <div class="modal-body bg-dark p-0">
                    <div class="embed-responsive embed-responsive-16by9">
                      <iframe class="embed-responsive-item" allowfullscreen></iframe>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    </div>
    
    <script>
    $(document).ready(function() {
        // Set iframe attributes when the show instance method is called
        $("#videoModal").on("show.bs.modal", function(event) {
            let button = $(event.relatedTarget); // Button that triggered the modal
            let url = button.data("video");      // Extract url from data-video attribute
            $(this).find("iframe").attr({
                src : url,
                allow : "accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
            });
        });
    
        // Remove iframe attributes when the modal has finished being hidden from the user
        $("#videoModal").on("hidden.bs.modal", function() {
            $("#videoModal iframe").removeAttr("src allow");
        });
    });
    </script>
    
    </body>
    </html>
    

    访问(链接断开):https://parrot-tutorial.com/run_code.php?snippet=bs4_modal_youtube

    【讨论】:

    • 如何在移动设备中旋转或制作全屏 youtube 视频?也许需要自动旋转才能使其成为横向。
    【解决方案4】:

    使用最新的Bootstrap 4.5+

    • 通过从 HTML 页面传递不同的 Youtube URL 来重复使用相同的模式
    • 具有出色的播放按钮图标和启用自动播放
    • 只需复制代码,您就完成了设置!!!
    • Codepen 中查看解决方案

        // javascript using jQuery - can embed in the script tag
    $(".video-link").click(function () {
      var theModal = $(this).data("target");
      videoSRC = $(this).attr("data-video");
      videoSRCauto = videoSRC + "?modestbranding=1&rel=0&showinfo=0&html5=1&autoplay=1";
      $(theModal + ' iframe').attr('src', videoSRCauto);
      $(theModal + ' button.close').click(function () {
        $(theModal + ' iframe').attr('src', videoSRC);
      });
    });
    #video-section .modal-content {
      min-width: 600px;
    }
    
    #video-section .modal-content iframe {
      width: 560px;
      height: 315px;
    }
    <!-- HTML with Bootstrap 4.5 and Fontawesome -->
    <html>
       <head>
          <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.1/css/all.css"
             integrity="sha384-xxzQGERXS00kBmZW/6qxqJPyxW3UR0BPsL4c8ILaIWXva5kFi7TxkIIaMiKtqV1Q" crossorigin="anonymous">
          <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
       </head>
    
       <body>
          <section id="video-section" class="py-5 text-center text-white">
             <div class="container h-100 d-flex justify-content-center">
                <div class="row align-self-center">
                   <div class="col">
                      <div class="card bg-dark border-0 mb-2 text-white">
                         <div class="card-body">
                            <a href="#" class="btn btn-primary bg-dark stretched-link video-link" data-video="https://www.youtube.com/embed/HnwsG9a5riA" data-toggle="modal" data-target="#video-modal">
                            <i class="fas fa-play fa-3x"></i>
                            </a>
                            <h1 class="card-title">Play Video</h1>
                         </div>
                      </div>
                   </div>
                </div>
             </div>
    
             <!-- Video Modal -->
             <div class="modal fade" id="video-modal" tabindex="-1" role="dialog">
                <div class="modal-dialog h-100 d-flex align-items-center">
                   <div class="modal-content">
                      <div class="modal-body">
                         <button type="button" class="close" data-dismiss="modal">
                         <span>&times;</span>
                         </button>
                         <iframe frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
                      </div>
                   </div>
                </div>
             </div>
          </section>
    
          <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
          <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
          <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
    
       </body>
    </html>
    
    
      [1]: https://codepen.io/richierich25/pen/yLOOYBL
    

    【讨论】:

    • 此代码适用于 vimeo 视频吗?或者需要一些改变
    【解决方案5】:

    对于处理视频、图像和页面的 Bootstrap 4...

    http://jsfiddle.net/c4j5pzua/

    $('a[data-modal]').on('click',function(){
    	var $page = $(this).data('page');
    	var $image = $(this).data('image');
    	var $video = $(this).data('video');
    	var $title = $(this).data('title');
    	var $size = $(this).data('size');
    	$('#quickview .modal-title').text($title);
    	if ($size) { $('#quickview .modal-dialog').addClass('modal-'+$size); }
    	if ($image) {
    		$('#quickview .modal-body').html('<div class="text-center"><img class="img-fluid" src="'+$image+'" alt="'+$title+'"></div>');
    	} else if ($video) {
    		$('#quickview .modal-body').html('<div class="embed-responsive embed-responsive-16by9"><iframe class="embed-responsive-item" src="https://www.youtube-nocookie.com/embed/'+$video+'?autoplay=1" allowfullscreen></iframe></div>');
    	}
    	if ($page) {
    		$('#quickview .modal-body').load($page,function(){
    			$('#quickview').modal({show:true});
    		});
    	} else {
    		$('#quickview').modal({show:true});
    	}
    	$('#quickview').on('hidden.bs.modal', function(){
    		$('#quickview .modal-title').text('');
    		$('#quickview .modal-body').html('');
    		if ($size) { $('#quickview .modal-dialog').removeClass('modal-'+$size); }
    	});
    });
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    
    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
    
    <div class="container my-4">
    
    <h3 class="mb-4">Bootstrap 4 Modal YouTube Videos, Images & Pages</h3>
    
    <a href="javascript:;" class="btn btn-primary" data-modal data-video="zpOULjyy-n8" data-title="Video Title" data-size="xl">Video</a>
    
    <a href="javascript:;" class="btn btn-primary" data-modal data-image="https://v4-alpha.getbootstrap.com/assets/brand/bootstrap-social-logo.png" data-title="Image Title" data-size="">Image</a>
    
    <a href="javascript:;" class="btn btn-primary" data-modal data-page="https://getbootstrap.com" data-title="Page Title" data-size="lg">Page *</a>
    
    <p class="mt-4">* Clicking this will break it, but it'll work using a local page!</p>
    
    </div>
    
    <div class="modal fade" id="quickview" tabindex="-1" role="dialog" aria-labelledby="quickview" aria-hidden="true">
    <div class="modal-dialog modal-dialog-scrollable modal-dialog-centered" role="document">
    <div class="modal-content">
    <div class="modal-header">
    <h5 class="modal-title">Title</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    </div>
    <div class="modal-body"></div>
    </div>
    </div>
    </div>

    【讨论】:

      【解决方案6】:

      Bootstrap 确实提供了开箱即用的模式弹出功能。

      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      
      <a class="btn btn-primary" data-toggle="modal" href="#modal-video"><i class="fa fa-play"></i> watch video</a>
      <div class="modal fade" id="modal-video" style="display: none;">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">close <i class="fa fa-times"></i></button>
            </div>
            <div class="modal-body">
              <iframe type="text/html" width="640" height="360" src="//www.youtube.com/embed/GShZUiyqEH0?rel=0?wmode=transparent&amp;fs=1&amp;rel=0&amp;enablejsapi=1&amp;version=3" frameborder="0" allowfullscreen=""></iframe>
              <p>Your video</p>
            </div>
          </div>
        </div>
      </div>

      【讨论】:

        【解决方案7】:

        $('#videoLink').click(function () {
            var src = 'https://www.youtube.com/embed/VI04yNch1hU;autoplay=1';
            // $('#introVideo').modal('show'); <-- remove this line
            $('#introVideo iframe').attr('src', src);
        });
        
        $('#introVideo button.close').on('hidden.bs.modal', function () {
            $('#introVideo iframe').removeAttr('src');
        });
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
        
        <!-- triggering Link -->
        <a id="videoLink" href="#0" class="video-hp" data-toggle="modal" data-target="#introVideo"><img src="img/someImage.jpg">toggle video</a>
        
        <!-- Intro video -->
        <div class="modal fade" id="introVideo" tabindex="-1" role="dialog" aria-labelledby="introductionVideo" aria-hidden="true">
          <div class="modal-dialog modal-lg">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              </div>
              <div class="modal-body">
                <div class="embed-responsive embed-responsive-16by9">
                    <iframe class="embed-responsive-item allowfullscreen"></iframe>
                </div>
              </div>
            </div>
          </div>
        </div>

        【讨论】:

        • 示例不运行
        【解决方案8】:

        好的。我找到了解决方案。

                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">
                                <span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                            </button>
                            <h3 class="modal-title" id="modal-login-label">Capital Get It</h3>
                            <p>Log in:</p>
                        </div>
        
                        <div class="modal-body">
        
                            <h4>Youtube stuff</h4>
        
        
        
                     <iframe src="//www.youtube.com/embed/lAU0yCDKWb4" allowfullscreen="" frameborder="0" height="315" width="100%"></iframe>       
                     </div>
        
        
                        </div>
        
                    </div>
                </div>
            </div>
        

        【讨论】:

          【解决方案9】:

          在另一个线程中发现了这个,它在桌面和移动设备上运行良好 - 这在其他一些解决方案中似乎并不正确。将此脚本添加到页面末尾:

          <!--Script stops video from playing when modal is closed-->
          <script>
              $("#myModal").on('hidden.bs.modal', function (e) {
                  $("#myModal iframe").attr("src", $("#myModal iframe").attr("src"));
              });
          </script>   
          

          【讨论】:

          • 这段代码对我有用。简单高效!谢谢!
          【解决方案10】:

          我也遇到了同样的问题 - 我刚刚添加了 font-awesome cdn 链接 - 注释掉下面的引导程序解决了我的问题.. 并没有真正解决它,因为 font awesome 仍然有效 -

          <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css" rel="stylesheet">
          

          【讨论】:

            【解决方案11】:

            user3084135 的回答对我来说很有效,但我也需要加入:

            1. 本地托管视频的“video”标签以及外部托管视频的“iframe”标签
            2. 确保源已被删除,但模式已被解除
            3. 该解决方案适用于 Bootstrap 响应式设计
            4. 所有视频在模式打开时自动播放
            5. 可以使用多种模式

            我完成的解决方案如下所示:

            模式触发按钮

            <a href="#" class="portfolio-link" data-toggle="modal" data-frame="iframe" data-target="#portfolioModal1" data-theVideo="http://www.youtube.com/embed/xxxxxxxx">
            

            data-frame 属性可以是“iframe”或“video”以反映适当的标签类型:iframe 用于外部 vid,video 用于本地托管。

            自举响应视频容器

            iFrame:

            <div align="center" class="embed-responsive embed-responsive-16by9">
              <iframe width="420" height="315" src="" frameborder="0" allowfullscreen></iframe>
            </div>
            

            视频:

            <div align="center" class="embed-responsive embed-responsive-16by9">
                <video width="640" height="364" controls>
                    <source src="" type="video/mp4">
                    Your browser does not support the video tag.
                </video>                               
            </div>
            

            这些都位于标准的 Bootstrap 响应式模态 div 中。

            JQUERY 脚本

            <script>
             $(document).ready(function(){
                function autoPlayModal(){
                  var trigger = $("body").find('[data-toggle="modal"]');
                  trigger.click(function() {
                    var theFrame = $(this).data( "frame" );
                    var theModal = $(this).data( "target" );
                    videoSRC = $(this).attr( "data-theVideo" ); 
                    if (theFrame == "iframe") {
                      videoSRCauto = videoSRC+"?autoplay=1" ;
                    } else {
                      videoSRCauto = videoSRC;
                      $("[id*=portfolioModal] video").attr('autoplay','true');
                    }
                    $(theModal+' '+ theFrame).attr('src', videoSRCauto); 
                    $("[id*=portfolioModal]").on('hidden.bs.modal', function () {
                        $("[id*=portfolioModal] "+ theFrame).removeAttr('src');
                    })
                  });
                }
            
              autoPlayModal();
            });
            </script>
            

            由于自动播放与 iframe 和视频标签的工作方式不同,因此使用条件来处理它们。为了允许多个模态,使用通配符选择器来识别它们(在我的例子中是 portfolioModal1-6)。

            【讨论】:

              【解决方案12】:

              使用$('#introVideo').modal('show'); 与引导程序正确触发冲突。当您单击打开 Modal 的链接时,它将在完成淡入淡出动画后立即关闭。 只需删除$('#introVideo').modal('show'); (使用引导程序 v3.3.2)

              这是我的代码:

              <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
              <!-- triggering Link -->
              <a id="videoLink" href="#0" class="video-hp" data-toggle="modal" data-target="#introVideo"><img src="img/someImage.jpg">toggle video</a>
              
              
              <!-- Intro video -->
              <div class="modal fade" id="introVideo" tabindex="-1" role="dialog" aria-labelledby="introductionVideo" aria-hidden="true">
                <div class="modal-dialog modal-lg">
                  <div class="modal-content">
                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    </div>
                    <div class="modal-body">
                      <div class="embed-responsive embed-responsive-16by9">
                          <iframe class="embed-responsive-item allowfullscreen"></iframe>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              
              
              <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"> </script>
              
              <script>
                //JS
              
              $('#videoLink').click(function () {
                  var src = 'https://www.youtube.com/embed/VI04yNch1hU;autoplay=1';
                  // $('#introVideo').modal('show'); <-- remove this line
                  $('#introVideo iframe').attr('src', src);
              });
              
              
              $('#introVideo button.close').on('hidden.bs.modal', function () {
                  $('#introVideo iframe').removeAttr('src');
              })
              </script>

              【讨论】:

                【解决方案13】:
                <a href="#" class="btn btn-default" id="btnforvideo" data-toggle="modal" data-target="#videoModal">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="315" src="https://www.youtube.com/embed/myvideocode" frameborder="0" allowfullscreen></iframe>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                

                【讨论】:

                  【解决方案14】:

                  这是另一个解决方案:Force HTML5 youtube video

                  只需将 ?html5=1 添加到 iframe 上的源属性,如下所示:

                  <iframe src="http://www.youtube.com/embed/dP15zlyra3c?html5=1"></iframe>
                  

                  【讨论】:

                  【解决方案15】:

                  我已经在wordpress模板上解决了:

                  $videoLink ="http://www.youtube.com/watch?v=yRuVYkA8i1o;".   
                  <?php
                      parse_str( parse_url( $videoLink, PHP_URL_QUERY ), $my_array_of_vars );
                      $youtube_ID = $my_array_of_vars['v'];    
                  ?> 
                  <a class="video" data-toggle="modal" data-target="#myModal" rel="<?php echo $youtube_ID;?>">
                      <img src="<?php bloginfo('template_url');?>/assets/img/play.png" />
                  </a>
                  
                  <div class="modal fade video-lightbox" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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">&times;</button>
                              </div>
                              <div class="modal-body"></div>
                          </div><!-- /.modal-content -->
                      </div><!-- /.modal-dialog -->
                  </div><!-- /.modal -->
                  
                  <script> 
                      jQuery(document).ready(function ($) {
                          var $midlayer = $('.modal-body');     
                          $('#myModal').on('show.bs.modal', function (e) {
                              var $video = $('a.video');
                              var vid = $video.attr('rel');
                              var iframe = '<iframe />';  
                              var url = "//youtube.com/embed/"+vid+"?autoplay=1&autohide=1&modestbranding=1&rel=0&hd=1";
                              var width_f = '100%';
                              var height_f = 400;
                              var frameborder = 0;
                              jQuery(iframe, {
                                  name: 'videoframe',
                                  id: 'videoframe',
                                  src: url,
                                  width:  width_f,
                                  height: height_f,
                                  frameborder: 0,
                                  class: 'youtube-player',
                                  type: 'text/html',
                                  allowfullscreen: true
                              }).appendTo($midlayer);   
                          });
                  
                          $('#myModal').on('hide.bs.modal', function (e) {
                              $('div.modal-body').html('');
                          }); 
                      });
                  </script>
                  

                  【讨论】:

                    【解决方案16】:

                    我在.modal.fade .modal-dialog 类上发现了与CSS3 转换(翻译)相关的这个问题(或我在https://github.com/twbs/bootstrap/issues/10489 发现和描述的问题)。

                    在 bootstrap.css 中,您会发现如下所示的行:

                    .modal.fade .modal-dialog {
                      -webkit-transform: translate(0, -25%);
                          -ms-transform: translate(0, -25%);
                              transform: translate(0, -25%);
                      -webkit-transition: -webkit-transform 0.3s ease-out;
                         -moz-transition: -moz-transform 0.3s ease-out;
                           -o-transition: -o-transform 0.3s ease-out;
                              transition: transform 0.3s ease-out;
                    }
                    
                    .modal.in .modal-dialog {
                      -webkit-transform: translate(0, 0);
                          -ms-transform: translate(0, 0);
                              transform: translate(0, 0);
                    }
                    

                    将这些行替换为以下内容将正确显示电影(在我的情况下):

                    .modal.fade .modal-dialog {
                      -webkit-transition: -webkit-transform 0.3s ease-out;
                         -moz-transition: -moz-transform 0.3s ease-out;
                           -o-transition: -o-transform 0.3s ease-out;
                              transition: transform 0.3s ease-out;
                    }
                    
                    .modal.in .modal-dialog {
                    
                    }
                    

                    【讨论】:

                    • 这不应该是公认的答案。不得编辑源 css 文件。
                    • 它们可以被覆盖(在一个新文件中),只需将每个字段设置为未设置。 transform: unset;
                    【解决方案17】:

                    我把这个 html/jQuery 动态 YouTube 视频模态脚本放在一起,当点击触发器(链接)时自动播放 YouTube 视频,触发器还包含要播放的链接。该脚本将找到本机引导模式调用,并使用来自触发器的数据打开共享模式模板。请参阅下文,让我知道您的想法。我很想听听想法...

                    HTML 模式触发器:

                     <a href="#" class="btn btn-default" data-toggle="modal" data-target="#videoModal" data-theVideo="http://www.youtube.com/embed/loFtozxZG0s" >VIDEO</a>
                    

                    HTML 模式视频模板:

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

                    JQUERY 函数:

                    //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);
                        });   
                      });
                    }
                    

                    函数调用:

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

                    小提琴: http://jsfiddle.net/jeremykenedy/h8daS/1/

                    【讨论】:

                    【解决方案18】:

                    如果您不想编辑引导 CSS 或以上所有内容都对您没有帮助(就像我的情况一样),有一个简单的修复方法可以让视频在 Firefox 上以模态运行。

                    您只需要从模式中删除“淡入淡出”类,它也会打开“in”类:

                    $('#myModal').on('shown.bs.modal', function () {
                        $('#myModal').removeClass('in');
                    });
                    

                    【讨论】:

                      【解决方案19】:

                      我有一个提示给你!

                      我会使用:

                      $('#myModal').on('hidden.bs.modal', function () {
                          $('#myModal iframe').removeAttr('src');
                      })
                      

                      代替:

                      $('#myModal button').click(function () {
                          $('#myModal iframe').removeAttr('src');
                      });
                      

                      因为您也可以在没有按钮的情况下关闭模态框,因此使用此代码,每次模态框隐藏时它都会删除视频。

                      【讨论】:

                      • 乍一看它可以工作,但如果你想重新打开这个模态怎么办。你不能因为你删除了'src'
                      【解决方案20】:

                      MMhh...您能否发布您的整个 HTML 文档以及您使用的浏览器/版本?

                      我重新创建了您的页面并在 3 种浏览器(Chrome、FF、IE8)中进行了测试。我能够毫无问题地停止和启动令人敬畏的 WDS4 预告片。这是我的代码:

                      <!DOCTYPE html>
                      <html>
                        <head>
                          <title>Bootstrap 101 Template</title>
                          <meta name="viewport" content="width=device-width, initial-scale=1.0">
                          <!-- Bootstrap -->
                          <link href="bootstrap.min.css" rel="stylesheet" media="screen">
                      
                          <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
                          <!--[if lt IE 9]>
                            <script src="../../assets/js/html5shiv.js"></script>
                            <script src="../../assets/js/respond.min.js"></script>
                          <![endif]-->
                        </head>
                        <body>
                          <div id="link">My video</div>
                      
                          <div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" 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">×</button>
                                      </div>
                      
                                      <div class="modal-body">
                                          <iframe width="400" height="300" frameborder="0" allowfullscreen=""></iframe>
                                      </div>
                                  </div>
                              </div>
                          </div>
                      
                          <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
                          <script src="jq.js"></script>
                          <!-- Include all compiled plugins (below), or include individual files as needed -->
                          <script src="bootstrap.min.js"></script>
                          <script>
                          $('#link').click(function () {
                              var src = 'http://www.youtube.com/v/FSi2fJALDyQ&amp;autoplay=1';
                              $('#myModal').modal('show');
                              $('#myModal iframe').attr('src', src);
                          });
                      
                          $('#myModal button').click(function () {
                              $('#myModal iframe').removeAttr('src');
                          });
                      </script>
                        </body>
                      </html>
                      

                      您可以尝试将模态播放器的 Z-Index 在堆栈中提高吗?

                      $('#myModal iframe').css("z-index","999");

                      【讨论】:

                      • 我试过你的代码,但是没有用。我无法点击视频中的任何按钮(暂停、音量、质量等)。你知道我不能使用任何 YOUTUBE 按钮吗?我在 Firefox 中查看它。 - 您使用的是哪个版本的 jquery?我使用的是 1.10.1 - 你使用的是 Bootstrap 3 吗?
                      • 继续我的测试:DON'T WORK in Firefox WORK in Chrome WORK in Safari
                      • 所以我使用你的代码,在任何地方测试它,它可以在任何地方工作,除了 Firefox。在 Mac 上,当我滚动按钮时,颜色会发生变化,但我无法单击它们。在 PC 上,屏幕保持黑色并且正在播放视频。它让我疯狂!我尝试使用安全模式运行 Firefox,清空缓存,没有任何效果。
                      • 我发现了同样的问题。对于 ubuntu 23.0 的 Firefox。但我收到一个错误 TypeError: Value not an object。 s.ytimg.com/yts/jsbin/www-embed-player-vflwWlnaY.js(第 220 行)。在 chrome 中,您的代码有效。
                      • Mmhhh.. 我已经在我的 Mac 上本地重新创建了它,它会尝试下载视频 ID。让我把它推到我的服务器上,看看是不是本地错误。
                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2015-05-08
                      • 1970-01-01
                      • 2013-12-29
                      • 2014-07-10
                      • 2014-10-11
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多