Book Image

Instant Fancybox

By : Kyle Diedrick
Book Image

Instant Fancybox

By: Kyle Diedrick

Overview of this book

Fancybox is a lightweight, highly customizable jQuery plugin for displaying modal dialogs. Fancybox is incredibly versatile; it works with all sorts of different content including images, videos, iFrames, custom HTML, and even SWF files. Fancybox is also very easy to customize, making it a great tool for any pop-up-like feature. Instant Fancybox is a hands-on guide which shows you how to use and customize the Fancybox plugin. The book provides step-by-step tutorials covering everything from simple installation to complex settings and features. This makes it a great way to get familiar with and ultimately master the Fancybox plugin. This book walks you through how to get the most out of the Fancybox plugin for jQuery, starting with the installation of the plugin and how to work with the JavaScript events that Fancybox triggers. You will learn everything you need to know about setting up Fancybox to show images, image galleries and slideshows, videos, and other content. You will also learn about the settings available within Fancybox and how to leverage them to make the Fancybox popup do exciting things. The Fancybox plugin has much to offer, and this book covers all of the features it provides. This book will provide you with all the information you will need to use the Fancybox plugin effectively.
Table of Contents (7 chapters)

Listening for Fancybox events (Advanced)


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.

Getting ready

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.

How to do it...

  1. To start with, let's make the bar hidden all the time. We can simply add the hidden class to the custom-controls div, as shown in the following code:

    <div class="custom-controls hidden">
  2. 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. The afterShow setting is an anonymous function, in which we remove the hidden class from the custom controls.

  3. 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 the hidden class back to the custom-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.

  4. 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.

How it works...

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

onCancel

Called to abort the call to open Fancybox

beforeLoad

Called just before Fancybox starts loading the content it is going to display

afterload

Called just after Fancybox finishes loading the content it is going to display

beforeShow

Called just before Fancybox starts opening

afterShow

Called just after Fancybox finishes opening

beforeClose

Called just before Fancybox starts closing

afterClose

Called just after Fancybox finishes closing

onUpdate

Called after the window is resized to change the orientation of the window (for tablets or phones), or when the user scrolls the window

onPlayStart

Called after the slideshow has started playing

onPlayEnd

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.