Pergunta

Posso abrir o vídeo do YouTube no FancyBox.

Eu tenho uma lista de links de vídeos do YouTube, para Ex:

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

e FancosBox:

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

Não quero criar para cada vídeo novo objeto de incorporação.

Existe algum plug -in ou outra maneira de fazer isso?

Foi útil?

Solução

Isso está quebrado, veja Editar

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

Dessa forma, se o usuário JavaScript estiver ativado, ele abrir um FancyBox com o vídeo incorporado do YouTube, se o JavaScript estiver desativado, ele abrirá a página do YouTube do vídeo. Se você quiser, você pode adicionar

target="_blank"

Para cada um dos seus links, ele não valida na maioria dos doces, mas abrirá o link em uma nova janela se o FancyBox não o pegar.

EDITAR

this, acima, não é referenciado corretamente, então o código não encontrará href debaixo this. Você tem que chamá -lo assim:

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

como coberto em http://fancybox.net/blog #4, replicado acima

Outras dicas

Comecei usando as respostas aqui, mas o modifiquei para usar o novo do YouTube iframe Incorporando ...

$('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
    });
});
$("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;

            }); 

Se você quiser adicionar a função AutoPlay. Simplesmente substitua

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

com

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

também você pode fazer o mesmo com o Vimeo

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

com

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

Isso tem uma expressão regular, por isso é mais fácil copiar e colar o URL do YouTube. É ótimo para quando você usa um CMS para clientes.

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

Obrigado, Alexander!

E para definir o botão Fancy-Close acima do conteúdo flash do YouTube, adicione 'Wmode' aos parâmetros 'SWF':

'swf': {'allowfullscreen':'true', 'wmode':'transparent'}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top