jQuery 使用 Fancybox 自动播放嵌入的 Youtube 视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5159047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:39:39  来源:igfitidea点击:

Autoplay embedded Youtube video with Fancybox

jqueryreplaceyoutubefancybox

提问by Joe W

I have on my page a linked image which when clicked loads up a Fancyboxmodal box. It does this successfully but I want the video to autoplay. If I set the YouTube URL to autoplay=1, the hidden DIV plays in the background on page load.

我在我的页面上有一个链接图像,单击该图像时会加载一个Fancybox模式框。它成功地做到了这一点,但我希望视频自动播放。如果我将 YouTube URL 设置为autoplay=1,则隐藏的 DIV 在页面加载时在后台播放。

// Here is the linked image
<a id="inline" href="#video"><img src="someimage.jpg" alt="Description" /></a>

// Here is the hidden DIV
<div style="display:none">
  <div id="video">
    <iframe title="YouTube video player" width="640" height="390" src="###YOUTUBE LINK###rel=0&amp;hd=1&amp;autoplay=0" frameborder="0"></iframe>
  </div>
</div>

// Here is the script
<script type="text/javascript">
  $("a#inline").fancybox({
    'hideOnContentClick': true,
  });
</script>

My guess is I need to do some sort of Bind()event and string replace to change autoplay=0to autoplay=1but I have tried a few variations without any success

我的猜测是我需要做某种Bind()事件和字符串替换来更改autoplay=0为,autoplay=1但我尝试了一些变体但没有任何成功

Any thoughts please?

请问有什么想法吗?

回答by Marcel

First change the hrefof your linked image to your YouTube URL making sure "autoplay=1" like this link(NOT the embed URL). Then get rid of your hidden <div>and follow tip 4 on the FancyBox "Tips & Tricks" pagewhich is fairly straight forward:

首先将href您的链接图片更改为您的 YouTube 网址,确保“ autoplay=1喜欢此链接(不是嵌入网址)。然后摆脱隐藏<div>遵循 FancyBox“提示和技巧”页面上的提示 4,这是相当直接的:

$("#tip4").click(function() {
    $.fancybox({
            'padding'       : 0,
            'autoScale'     : false,
            'transitionIn'  : 'none',
            'transitionOut' : 'none',
            'title'         : this.title,
            'width'     : 680,
            'height'        : 495,
            'href'          : this.href.replace(new RegExp("watch\?v=", "i"), 'v/'),
            'type'          : 'swf',
            'swf'           : {
                 'wmode'        : 'transparent',
                'allowfullscreen'   : 'true'
            }
        });

    return false;
});

Doing it this way, people still get to watch the video, even if JS is disabled for them.

这样做,人们仍然可以观看视频,即使他们禁用了 JS。