Make Vimeo Lightbox Auto-Closes on Finish Using FancyBox

FancyBox has been my number one preference when I need to implement a jQuery-based lightbox on any web page I am working on. It is the most customizable and extensible jQuery plugin for this function.

Since version 2.0, it also introduced a concept of ‘helpers’ to reduce the size of the main JS file and also to allow developers to play more with its functionality with less risk of breaking the core functions when they are updated. Below I am going to show a little trick to modify FancyBox’s media helper to allow Vimeo link to auto-close when the video finishes.

Vimeo API

The first step to achieve this is by understanding Vimeo Player JavaScript API and how to enable API calling for the videos. To save your time, there are two items you need to note:

  1. You need to turn on the API using api=1 code and define the player ID ie. player_id=vimeoplayer
  2. Use Froogaloop, a JS mini-library written by Vimeo to communicate with their API. It can be downloaded here.

Modifying Fancybox media helper file

By default, the media helper file from FancyBox does not define the API and player ID. So we need to make minor change with the helpers/jquery.fancybox-media.js:

 

That’s it. You may want to save the helper file to another name, like jquery.fancybox-media.custom.js

Putting them together

First call FancyBox scripts like you usually do, including the customized media helper file and Froogaloop.

Next, let’s try to call FancyBox that auto-closes when the video has finished playing:

Done.

2 Comments Make Vimeo Lightbox Auto-Closes on Finish Using FancyBox

Leave a Reply