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)

Customizing other helpers (Intermediate)


The image title and transparent overlay are also Fancybox helpers. Unlike the other helpers that we have been working with, these are included in the default application. Each of these helpers has a few settings that we can use.

Getting ready

The title has only one setting, the type setting. The type setting of the title determines where it positions on the image. The overlay has five settings: closeClick, speedOut, showEarly, css, and locked. We will adjust all of these settings to see how it impacts the functionality of Fancybox.

How to do it...

  1. Change the title helper's type setting to be over.

    $('.fancybox').fancybox({
      helpers: {
        title: {
          type: 'over'
        }
      }
    });

    The type setting for the title can also be float, inside, and over. The default option is float. You can change the type setting to be whichever you prefer; I would recommend trying all of them and seeing which one you like best. You can also turn off the title by setting the title setting to null:

    $('.fancybox').fancybox({
      helpers: {
        title: null
      }
    });
  2. Change the overlay helper's closeClick setting to false.

    The closeClick setting allows the user to click the overlay to close the Fancybox pop up when it is set to true. If it is set to false, clicking on the overlay does nothing.

  3. Change the speedOut setting to 1000.

    The speedOut setting determines how quickly the overlay should disappear. The number provided is the time in milliseconds that the animation will take, so in our case the overlay will disappear in one second (1000 milliseconds).

  4. Change the showEarly setting to be false.

    The showEarly setting will display the overlay immediately if it is set to true. If it is set to false, the overlay will only show once the content is loaded in the Fancybox pop up.

  5. Change the locked setting to false.

    The locked setting will prevent the content from showing a scrollbar when displaying a Fancybox pop up. I would recommend leaving this set to true at all times, so that the user cannot scroll while Fancybox is displaying. Allowing the user to scroll while Fancybox is displaying could result in the user not seeing the Fancybox pop up at all.

    With all of these settings applied, our call to Fancybox looks like the following code:

    $('.fancybox').fancybox({
      helpers: {
        overlay: {
          closeClick: false,
          speedOut: 1000,
          showEarly: false,
          locked: false
        }
      }
    });

    And now we have a Fancybox pop that cannot be clicked on to close the overlay, takes one second to animate back to being hidden, does not show the overlay until the content finishes loading, and does not lock the window size to the overlay area.

  6. It is also possible to turn off the overlay entirely by setting it to null, similar to the title:

    $('.fancybox').fancybox({
      helpers: {
        overlay: null
      }
    });

How it works...

Fancybox provides us with a range of settings that we can configure for the overlay. We can also adjust the positioning of the title for our images. This functionality is all built into the Fancybox plugin.

There's more...

The other setting that is provided for the overlay is css. This setting allows us to define specific styles that we want to use on the overlay.

Adding CSS to the overlay

The overlay css setting is useful for applying custom styles to the overlay. A simple one is instead of a black transparent overlay, we want a white transparent overlay. We can use the css setting to apply the background style to the overlay, shown as follows:

$('.fancybox').fancybox({
  helpers: {
    overlay: {
      css: {
        background: "rgba(255, 255, 255, 0.7)"
      }
    }
  }
});

In the given code, we have provided the css property on the overlay settings object with a background of rgba(255, 255, 255, 0.7), which is the equivalent of a white background at 70 percent opacity. Styles are applied using the same object structure that would be passed to the jQuery css method.