Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoplay embedded Youtube video with Fancybox

I have on my page a linked image which when clicked loads up a Fancybox modal 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.

// 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=0 to autoplay=1 but I have tried a few variations without any success

Any thoughts please?

like image 402
Joe W Avatar asked Mar 01 '11 19:03

Joe W


1 Answers

First change the href of 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" page which is fairly straight forward:

$("#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;
});
  • Quick example

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

like image 131
Marcel Avatar answered Nov 19 '22 20:11

Marcel