Fancybox provides us with a number of different event-driven callback functions that allow us to run code when a certain event occurs. The main callback functions we can use allow us to run code when Fancybox is about to display or hide, when content is about to be loaded or after it is done loading, and when the user plays or pauses the slideshow. We are only going to look at triggering functionality when Fancybox opens and closes and on play/pause. At the end of this recipe is a list of all of the callback functions available in Fancybox.
Let's expand upon the code that we used in the previous recipe. Right now the bar with our links shows all the time regardless of whether or not Fancybox is displaying. Let's make it such that this bar shows and hides when Fancybox is opened and closed. Let's also make Fancybox close when the slideshow is paused.
To start with, let's make the bar hidden all the time. We can simply add the
hidden
class to thecustom-controls
div, as shown in the following code:<div class="custom-controls hidden">
Now that the custom controls are hidden on page load, we need to make them display when we open Fancybox. We'll use the
afterShow
callback to make the custom controls show right after Fancybox has finished animating into view. We need to change our call to Fancybox to include this new callback function:$('.fancybox').fancybox({ afterShow: function() { $('.custom-controls').removeClass('hidden'); } });
Here we are calling Fancybox with a settings object that only has one setting:
afterShow
. TheafterShow
setting is an anonymous function, in which we remove thehidden
class from the custom controls.Now our controls show once we open Fancybox, but they continue to show after it is closed. We can use the
beforeClose
callback to run the code to add thehidden
class back to thecustom-controls
div just as the Fancybox pop up is closing, as shown in the following code:$('.fancybox').fancybox({ afterShow: function() { $('.custom-controls').removeClass('hidden'); }, beforeClose: function() { $('.custom-controls').addClass('hidden'); } });
The
custom-controls
div should now hide when Fancybox is closed.Next let's make the Fancybox pop up close when the slideshow is paused. Fancybox has another callback function
onPlayEnd
, which is called when slideshow playback is paused. We'll use this along with the$.fancybox.close()
API function to make the Fancybox pop up close when the slideshow is paused, as shown in the following code:$('.fancybox').fancybox({ afterShow: function() { $('.custom-controls').removeClass('hidden'); }, beforeClose: function() { $('.custom-controls').addClass('hidden'); }, onPlayEnd: function() { $.fancybox.close(); } });
In the previous code, we are telling Fancybox that the
onPlayEnd
callback should use the anonymous function, which closes the Fancybox pop up. When you play the slideshow nothing should happen, but if you click the link again to pause, the slideshow Fancybox will close.
The functions we used here are event driven. This means that behind the scenes, Fancybox is triggering an event when these actions complete and it has an event binding (listens for the event), which then runs the function we provided.
There are more events that we can listen for and are used in the exact same way as the ones we set up in the example. The entire list can be found in the following table, or on the Fancybox website: http://fancyapps.com/fancybox/#docs.
Event |
Description |
---|---|
|
Called to abort the call to open Fancybox |
|
Called just before Fancybox starts loading the content it is going to display |
|
Called just after Fancybox finishes loading the content it is going to display |
|
Called just before Fancybox starts opening |
|
Called just after Fancybox finishes opening |
|
Called just before Fancybox starts closing |
|
Called just after Fancybox finishes closing |
|
Called after the window is resized to change the orientation of the window (for tablets or phones), or when the user scrolls the window |
|
Called after the slideshow has started playing |
|
Called after the slideshow has been paused |
Fancybox is a powerful plugin for jQuery, but it is not the only one. If you want to learn more about other great jQuery plugins, check out http://plugins.jquery.com/. If you want to learn how to write your own jQuery plugin, a great starting point is on the jQuery site as well at http://learn.jquery.com/plugins/. The jQuery site also has information on events and how to contribute to jQuery, and many other resources at http://www.jquery.com.