Using Fancybox with YouTube Videos

Written by on December 2, 2010 in jQuery - 16 Comments
vid

12/9/12 UPDATE: Check out the new version of this post, with the updated version of fancyBox which allows for responsive, size adjusting videos.

It’s easy enough to copy the code that YouTube gives you to simply embed a video on your website. But what if you have a lot of videos that need to go on one page? It would be a lot cleaner to have smaller images of the videos; which you could click on to watch the video, all without leaving the webpage. Fancybox is an easy solution to this, checkout the demo to see how clean this method looks.

Here’s the code to use to make this happen, I placed it in an external javascript file in the demo version.

jQuery(document).ready(function() {

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

		return false;
	});
});

Then simply add the image to the page and call fancybox like this:

  <a class="video"  title="The Falltape" href="http://www.youtube.com/v/ZeStnz5c2GI?fs=1&amp;autoplay=1"><img src="images/1.jpg" alt="" /></a>

In order to add your own video, just copy the link from the YouTube video you want and replace it in the code above. One extra thing I did was to add: autoplay=1 at the end of the code that I copied from YouTube. This is so that when the video opens over the screen it will automatically start playing. If somebody clicked the thumbnail of the video they obviously want to watch the video, so why make them click play twice?

Feel free to grab the demo to speed up your coding time.

Sponsors

About the Author

Mike Ilsley "ilz" is a Boston based website designer, front-end developer, WordPress addict, owner of Beantown Design, and exclusive ThemeForest author. Follow him on Twitter and Facebook, and don't forget to subscribe to the WebDesign&Such email RSS feed. Also, check out his latest project, jQueryMobileShowcase.com.


  • luis

    Hey, nice post, could you explain the regular expression stuff (line 12 in the code)?

  • Gary

    That’s a Regex to change the youtube URL from:

    “http://www.youtube.com/watch?v=l-sw4i06Lbw”

    to

    “http://www.youtube.com/v/l-sw4i06Lbw”

    You can check the difference by clicking the URL’s above. These are valid links to a valid youtube video.

  • Suzy

    Thanks for this! came in handy today!

  • http://www.appware.co.uk Remi

    This post is just regurgitated from the fancybox website blog and doesn’t work JUST LIKE the fancybox blog post.

    If you are having issues you must change the youtube URL from http://www.youtube.com/watch?=************ to http://www.youtube.com/v/************

    Even though the javascript is supposed to do this for you it does not work.

    • mike ilz

      What isn’t working for you? Works perfectly for me.

      • Max

        Yeah works great for me too.. Super great.

  • http://www.appware.co.uk Remi

    The line which replaces ‘/watch?=’ does not work as expected, fancybox opens but the video doesn’t load. As I previously said I just changed the video URL myself and it works fine.

  • http://twitter.com/SandraLV86 Alfredo Goodlow

    This ended up being quite useful – initially I did not agree with this though in the end it turned out fantastic. Beneficial for each and every man and woman … if you think that this data wasn’t enough, read it once again and you will definitely see. My personal sex life simply exploded – dumbfounded!!

  • http://alexmatveev.me Alex Matveev

    Embedding youtube players which don’t open new window when clicked (like http://www.youtube.com/embed/) is not working.

  • Noel

    this is what i am looking for.. but with a twist…

    I have a question, according to your demo, its all videos that is in.

    Is it possible to have mix videos and image in it?

    Say the first thumbnail is link to youtube video(same in demo)..the second thumbnail is link to an image..

  • http://twitter.com/mariuszwozniak Mariusz

    Works like a charm, thanks for sharing!

  • Aloykatos

    Even thought i typed allowfullscreen as true i cant go fullscreen. Can you help me?

  • http://www.festivaloranjewoud.nl Yoram

    How do I change the background (now dark) into none (100% transparent)?

    • http://www.festivaloranjewoud.nl Yoram

      Seems I found out:

      ‘overlayOpacity’: 0.1,