How to show YouTube videos in a lightbox.
- August 7th, 2010
- Write comment
This week a client wanted a more elegant way to display video on their site and asked us to try and come up with the solution. I was already using Fancybox, the amazing jquery lightbox alternative plugin, so I set out the see if there was a recommended approach for this. I didn’t have to look far because on the Fancybox website, they have a great Tricks & Tips section! It had been a few months since I really looked around on the site, so I’m not sure when they added this (it may have already existed in some form or fashion). Anyways, I’ll cut to the chase.
Since our client has a YouTube channel and they are primarily going to be displaying those videos, I wanted to make it easy on them. This approach is just right, because they don’t have to remember much when they’re adding content in the WYSIWYG, they literally just have to set the href on any link to the youtube video they want to display in the lightbox. Using the following javascript (don’t forget to include your fancybox js and css files) any link including “youtube.com/watch” will be called in a lightbox! Pretty cool huh? Want to see some examples? Check out the demo I set up!
$(document).ready(function(){
$('a[href*="youtube.com/watch"]').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 you can see, there are several variables you can change and adjust – the size, whether to show the title and how it will transition in and out. So, like I said, I found the examples for this on Fancybox.net’s Tips & Tricks page. Check it out!
If you want to set this up, here’s all you need:
FancyBox jQuery plugin
View the Demo:







