33

我可以在 Fancybox 中打开 YouTube 视频吗?

我有一个 YouTube 视频链接列表,例如:

<a href="http://www.youtube.com/watch?v=PvbqV8W96D0" class="more">Play</a>

和花式盒:

$("a.more").fancybox({
                    'titleShow'     : false,
                    'transitionIn'  : 'elastic',
                    'transitionOut' : 'elastic'
        });

我不想为每个视频创建新的嵌入对象。

是否有一些插件或其他方式来做到这一点?

4

6 回答 6

35

这是坏的,见编辑

<script type="text/javascript">
$("a.more").fancybox({
                    'titleShow'     : false,
                    'transitionIn'  : 'elastic',
                    'transitionOut' : 'elastic',
            'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
            'type'      : 'swf',
            'swf'       : {'wmode':'transparent','allowfullscreen':'true'}
        });
</script>

这样,如果启用了用户 javascript,它会打开一个带有 youtube 嵌入视频的幻想框,如果禁用 javascript,它会打开视频的 youtube 页面。如果你愿意,你可以添加

target="_blank"

对于您的每个链接,它不会在大多数文档类型上验证,但如果fancybox 没有选择它,它将在新窗口中打开链接。

编辑

this,上面,没有被正确引用,所以代码hrefthis. 你必须这样称呼它:

$("a.more").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;
});

http://fancybox.net/blog #4 所述,在上面复制

于 2010-03-30T12:27:32.053 回答
22

我开始使用这里的答案,但修改它以使用 YouTube 的新iframe嵌入......

$('a.more').on('click', function(event) {
    event.preventDefault();
    $.fancybox({
        'type' : 'iframe',
        // hide the related video suggestions and autoplay the video
        'href' : this.href.replace(new RegExp('watch\\?v=', 'i'), 'embed/') + '?rel=0&autoplay=1',
        'overlayShow' : true,
        'centerOnScroll' : true,
        'speedIn' : 100,
        'speedOut' : 50,
        'width' : 640,
        'height' : 480
    });
});
于 2011-09-22T14:13:55.743 回答
21
$("a.more").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',    // <--add a comma here
                  'swf'                 : {'allowfullscreen':'true'} // <-- flashvars here
                  });
                 return false;

            }); 
于 2010-04-05T18:15:12.803 回答
5

如果你想添加自动播放功能。只需更换

this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'), 

this.href = this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + '&autoplay=1',

你也可以用 vimeo 做同样的事情

this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),

this.href = this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1') + '&autoplay=1',
于 2010-08-19T16:09:59.210 回答
2

这有一个正则表达式,因此只需复制和粘贴 youtube 网址就更容易了。当您为客户使用 CMS 时非常适合。

/*fancybox yt video*/
$(".fancybox-video").click(function() {
    $.fancybox({

    padding: 0,
        'autoScale'     : false,
        'transitionIn'  : 'none',
        'transitionOut' : 'none',
        'title'         : this.title,
        'width'         : 795,
        'height'        : 447,
        'href'          : this.href.replace(new RegExp("watch.*v=","i"), "v/"),
        'type'          : 'swf',
        'swf'           : {
        'wmode'             : 'transparent',
        'allowfullscreen'   : 'true'
         }

    });
    return false;
});
于 2012-11-13T03:19:24.507 回答
1

谢谢,亚历山大!

并在 youtube 的 flash 内容上方设置花式关闭按钮,将“wmode”添加到“swf”参数:

'swf': {'allowfullscreen':'true', 'wmode':'transparent'}
于 2010-12-14T10:05:33.727 回答