【问题标题】:Stop Youtube from playing when Modal is closed模态关闭时停止播放 Youtube
【发布时间】:2015-07-29 07:31:52
【问题描述】:

我见过一些类似的问题,但不幸的是,没有一个答案能完全解决我的问题。我有多个模态,每个模态都有不同的视频 iFrame。我希望视频在模式关闭时停止播放。到目前为止,我尝试使用 jQuery 在用户关闭模式时停止视频。它有效,但由于某种原因,所有视频都被我的代码中列出的第一个视频替换。有点奇怪。你可以在这里看到它:http://tiffanymackins.com/gentrification/ 我没有使用 Bootstrap 或任何框架。非常感谢任何帮助。

这是我正在使用的代码的 sn-p:

<a href="#modal-soulshine"><img id="soulshine-pin" class="pins" src="images/redPin.png"></a> 
    <div id="modal-soulshine" class="modal">
       <div>
            <a href="#close" title="Close" class="close"> close </a>
            <h2> Soulshine Organics Farm: Jesse Leadbetter and Nicole Argento</h2>
            <span class="ss-info"> Moved to Belmont in 2009</span>
           <iframe id="player" width="450" height="253" src="https://www.youtube.com/embed/EpDEd9R005k" frameborder="0" allowfullscreen></iframe>

           <p>....</p>
       </div>     
    </div>

这是阻止视频播放的 jQuery。:

/*Function built to take on search for youtube video in modal*/
function ytplayer() {
    $('.modal iframe').attr("src", $(".modal iframe").attr(
        "src"));
}  
/*Function shuts down video when dismiss button is toggled*/
$('.close').click(function() {
    $('#player').stop(this);
    ytplayer();
});

【问题讨论】:

  • 如果您不使用 Youtube API,则必须从源属性中删除视频,如 $('.modal iframe').attr("src", "");
  • 嗯,在我关闭模式后,这似乎只是摆脱了视频。
  • 嗯,是的,这不是重点吗?在您无法控制的 iFrame 中停止视频的唯一方法是更改​​ iFrame 的源以删除内容。
  • 感谢您的回答,但关键是要停止视频,以便在选择另一个模式和视频时它不会在后台播放。
  • 除非您使用 Youtube API 并且可以访问视频,否则您不能这样做。

标签: jquery youtube modal-dialog


【解决方案1】:

试试看

$( '#modal-soulshine').on( 'hidden.bs.modal', function (e) {

    $( '#modal-soulshineiframe' ).removeAttr( 'src' );

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 2013-05-21
    相关资源
    最近更新 更多