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)

Interacting with Fancybox (Advanced)


Fancybox provides a number of methods that we can use to programmatically interact with the Fancybox pop up. Let's take a look at some of these methods and at what they allow us to do.

Getting ready

To make things simple, we are just going to use a single-image pop up like the one found in the Installing Fancybox recipe. We'll be looking at the pieces of the API that allow us to interact with just the Fancybox pop up in this recipe. We'll cover manually opening and closing Fancybox, the update and toggle functions, the reposition function, the show and hide loader functions, and the close function.

How to do it...

  1. Let's start with opening the Fancybox pop up using the API. We used this briefly in the recipe on changing slideshow settings. The method is $.fancybox.open and can be passed a range of different parameters. The first parameter is the content to open inside of Fancybox and the second parameter is the options we want to use for this Fancybox pop up. If we want to open Fancybox using the API method and have it do the exact same thing as in the Fancybox with a single image recipe, we'll call Fancybox and pass it the href tag of the image as shown in the following code:

    $('#show-fancybox').click(function() {
      $.fancybox.open('images/waterfall.png');
    });

    The previous code uses a jQuery click event binding, which is passed an anonymous function, to tell Fancybox to open and use the string we provided to open. The string can be a range of different types of content that Fancybox can open. If you wanted it to open an image gallery, images can be passed in as an array, as shown in the code:

    $('#show-fancybox').click(function() {
      $.fancybox.open([
        {href:'images/waterfall.png'},
        {href:'images/frozen-lake.png'}
      ]);
    });

    The objects can contain the href and title tags for the image, as shown in the following code:

    {href:'images/frozen-lake.png', title: "Frozen Lake"}

    For the remainder of this recipe, we will have Fancybox load some HTML. Add this HTML to the index.html file inside the body:

    <div id="fancybox-content" class="hidden">
      <h2>Super Simple Fancybox Popup</h2>
      <p>With some basic text for content.</p>
      <a href="#" class="close">Close Fancybox</a>
    </div>

    We also need to slightly change the link to open Fancybox in the index.html file:

    <a id="show-fancybox" href="#">Show The HTML</a>

    Lastly, we need to change our open function for Fancybox inside the scripts.js file:

    $('#show-fancybox').click(function() {
      $.fancybox.open($('#fancybox-content'));
    });

    Now our Fancybox open method contains a jQuery object for the content to display. When you open Fancybox it should look like the following screenshot:

  2. Now that we have Fancybox opening our HTML, let's look at using the $.fancybox.close function. This function closes the Fancybox pop up. Let's make the Close Fancybox link we have inside the Fancybox pop up, close the pop up.

    $('.close').on('click', function() {
      $.fancybox.close();
    });
  3. Fancybox has other functions that are advantageous when you have dynamically changing content inside the Fancybox pop up. If you want to tell Fancybox to reposition the location of the pop up, it can be done with a call to the $.fancybox.reposition() function. Fancybox will move the pop up to the appropriate location inside the window.

  4. Fancybox also provides us with a function to resize the Fancybox window as required. The $.fancybox.update() function tells Fancybox to resize the height of the Fancybox pop up to match the content. It is necessary when you are programmatically changing the content inside the Fancybox pop up.

  5. The $.fancybox.toggle() function will put Fancybox into fullscreen mode, if necessary. It will only have an effect if the content is larger than the current Fancybox pop-up window.

  6. You can show and hide the Fancybox loader using the $.fancybox.showLoading() and $.fancybox.hideLoading() functions. These functions are useful when you are processing data inside the Fancybox pop up and want to tell the user that your content or information is loading.

  7. The last major function available to us is the $.fancybox.cancel() function. This function will stop loading the Fancybox pop up. It may be useful when the content takes too long to load and you want to just stop (especially if the content is an iframe and the website you are trying to load is not available).

How it works...

The Fancybox API is simply a set of functions that the plugin allows us to call. These functions are exposed to make it easier for us to work with the plugin and have increased control over how the plugin works.

You may have noted that when we created the close link we bound to the click event using the jQuery .on() function. This is because when Fancybox moves the HTML into the Fancybox pop up, the events that have been bound to the objects are reset. By using the .on() function, the event is listened for at the window level (similar to the jQuery .live() function); so no matter where a link with the class of close exists, the event binding will trigger. For more information on event binding and delegation using jQuery, see the jQuery API at http://api.jquery.com/on/.