Open youtube video in Fancybox jquery

前端 未结 6 1635
有刺的猬
有刺的猬 2020-12-02 07:55

Can I open youtube video in fancybox.

I have a list of youtube videos links , for ex:



        
相关标签:
6条回答
  • 2020-12-02 08:28
    $("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;
    
                }); 
    
    0 讨论(0)
  • 2020-12-02 08:36

    If you wanna add autoplay function to it. Simply replace

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

    with

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

    also you can do the same with vimeo

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

    with

    this.href = this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1') + '&autoplay=1',
    
    0 讨论(0)
  • 2020-12-02 08:43

    This has a regular expression so it's easier to just copy and paste the youtube url. Is great for when you use a CMS for clients.

    /*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;
    });
    
    0 讨论(0)
  • 2020-12-02 08:45

    THIS IS BROKEN, SEE EDIT

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

    This way if the user javascript is enabled it opens a fancybox with the youtube embed video, if javascript is disabled it opens the video's youtube page. If you want you can add

    target="_blank"
    

    to each of your links, it won't validate on most doctypes, but it will open the link in a new window if fancybox doesn't pick it up.

    EDIT

    this, above, isn't referenced correctly, so the code won't find href under this. You have to call it like this:

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

    as covered at http://fancybox.net/blog #4, replicated above

    0 讨论(0)
  • 2020-12-02 08:46

    I started by using the answers here, but modified it to use YouTube's new iframe embedding...

    $('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
        });
    });
    
    0 讨论(0)
  • 2020-12-02 08:49

    Thanx, Alexander!

    And to set the fancy-close button above the youtube's flash-content add 'wmode' to 'swf' parameters:

    'swf': {'allowfullscreen':'true', 'wmode':'transparent'}
    
    0 讨论(0)
提交回复
热议问题