Follow these steps to set up Colorbox to play a set of videos:
<p><a href="http://www.youtube.com/embed/2_HXUhShhmY?autoplay=1" id="video-link">Watch the video</a></p>
Note a couple of things about my video link. First, I'm using the embed URL for the video rather than the link to YouTube's video page. For users without JavaScript enabled, this will take them to a stand-alone video player page on YouTube's site. For users with JavaScript enabled, it will ensure that only the video player is loaded into the Colorbox rather than the full YouTube video page. Second, I'm adding a parameter to the URL for the video, setting autoplay
to 1. This is how you can make embedded YouTube videos automatically play when the site visitor views your page. It's generally a bad idea to have a video autoplay, but in this case, the user will have already clicked a link that says Watch the video, so it seems like a safe bet that they'll be expecting a video to play once they've clicked that link.
scripts.js
file and get set to write up our custom JavaScript. We'll get started with the document ready statement:$(document).ready(function(){ });
colorbox()
method:$(document).ready(function(){ $('#video-link').colorbox(); });
But if we refresh the page in a browser and attempt to view the video, we get an error. That's because we're attempting to load in the video via Ajax, and because of browser security restrictions, we can't make asynchronous requests to a different server. In this case, we're trying to make a call to http://youtube.com, but that's not where our Colorbox page is hosted, so the browser blocks our request.
iframe
and load our external content into the iframe
. And also luckily, Colorbox provides a way for us to do so easily. We'll just pass a key/value pair to the colorbox()
method setting iframe
to true
like the following:$('#video-link').colorbox({
iframe: true
});
Now our video loads into the Colorbox, but the Colorbox has no idea how large our video can be, so we can't see it.
innerWidth
and innerHeight
. We're using innerWidth
and innerHeight
rather than width and height in this case because we're passing in how large we want the video player (or content) to be, rather than how large we want the Colorbox to be:$('#video-link').colorbox({
iframe: true,
innerWidth: '640px',
innerHeight: '480px'
});
index.html
and add a list of favorite videos to our page instead of just one link to a video. We'll use a rel
attribute set to favorites
for each one and provide a title
attribute so our videos will display a caption underneath:<h3>Favorite Videos</h3> <ul> <li><a href="http://www.youtube.com/embed/itn8TwFCO4M?autoplay=1" rel="favorites" title="Louis CK and Everything is Amazing">Everything is Amazing</a></li> <li><a href="http://www.youtube.com/embed/UN0A6h9Wc5c?autoplay=1" rel="favorites" title="All This Beauty by The Weepies">All This Beauty</a></li> <li><a href="http://www.youtube.com/embed/ZWtZA-ZmOAM?autoplay=1" rel="favorites" title="ABC's That's Incredible">That's Incredible</a></li> </ul>
scripts.js
is to update the selector. Instead of selecting one single link by ID, we're going to select our set of favorites links by their rel
attribute:$('a[rel="favorites"]').colorbox({
iframe:true,
innerWidth:'640px',
innerHeight: '480px'
})
If you view the page in the browser, you'll see that you have a caption under the video and next and previous buttons that allow you to navigate between the videos without closing the Colorbox.
current
key:$('a[rel="favorites"]').colorbox({
iframe:true,
innerWidth:'640px',
innerHeight: '480px',
current: 'Video {current} of {total}'
})
Now, our pagination indicator correctly reads Video 1 of 3. Our site visitors can easily move from video to video without having to close the Colorbox and each video displays a caption:
What just happened?
We learned how to create both a stand-alone video player and a multiple video player inside a Colorbox. We learned how to pass in key/value pairs to tell the Colorbox to load in external content in an iframe
, working around cross-domain Ajax restrictions. We also learned how to modify the pagination indicator text to fit our current content type. We used the innerWidth
and innerHeight
keys to set the video player's size.
3.15.144.170