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)

Modifying styles and layout (Intermediate)


Now that we have a basic handle on how Fancybox works and some of the things it can do, let's start looking at modifying the way the Fancybox pop up looks. Fancybox provides a number of useful settings we can use to change the way that the Fancybox pop up looks. We're going to start with the basic margin and height settings and work our way to the auto-sizing settings.

Getting ready

To get things started, we just need a simple Fancybox pop up. In the example code, I have copied the code from the Fancybox with a single image recipe, where we used Fancybox to load a single image.

How to do it...

  1. The Fancybox margins indicate the minimum distance between the Fancybox pop up and the edge of the window. To change the margins of the Fancybox pop up, we provide it a number. This sets all of the margins to be the same, as shown in the following code:

    $('#show-fancybox').fancybox({
      margin: 50
    });

    You can also set the margins individually by using an array. The order of the values in the array is as follows: [top, right, bottom, left]. For example, say we wanted to have the margin on the left higher than the margin on the right, as shown in the following code:

    $('#show-fancybox').fancybox({
      margin: [50, 50, 50, 200]
    });

    Now the Fancybox pop up will be, at minimum, 200 pixels from the left edge of the window. The default margin is 20 pixels.

  2. Padding is the space inside the Fancybox pop up between the edge of the pop up and the content. Padding can be set exactly the same way as margins: either as a number that determines the padding on all sides, or as an array that sets the padding on each side individually. If we did not want any padding, we would set it as shown in the following code:

    $('#show-fancybox').fancybox({
      padding: 0
    });

    The default padding value is 20 pixels.

  3. Fancybox can be told whether or not we want it to automatically size to the content we provide. If we set the autoSize setting to false, the Fancybox pop up will become the default height and width. Note that the autoSize setting does not apply to images; it only applies to every other type of content that Fancybox will display. The default setting for autoSize is true.

    We can also provide the height and width for the Fancybox pop up. However, these two properties only work with the iframe and SWF content, unless we set the autoSize setting to false. Let's set the autoSize setting to false, the height to 300, and the width to 200 and load the Fancybox website in the Fancybox pop up, as shown in the following code:

    $('#show-fancybox').fancybox({
      padding: 0,
      autoSize: false,
      height: 300,
      width: 250
    });

    Here we have set the padding to 0, the autoSize setting to false, the height to 300, and the width to 200. This results in a very small window that has loaded the Fancybox website, as seen in the following screenshot:

    The default height setting is 600 pixels and the default width setting is 800 pixels.

  4. Fancybox also provides the minWidth, minHeight, maxWidth, and maxHeight settings. These settings do exactly what one would expect them to do: set the minimum or maximum height and width of the Fancybox pop up. Note that setting maximums and minimums does apply to images, so if you need to specify the height or width of the pop up that displays images, the following settings are the ones to use:

    $('#show-fancybox').fancybox({
      padding: 0,
      maxHeight: 400,
      maxWidth: 300,
      minHeight: 250,
      minWidth: 250
    });

    In the previous code, we set a maximum height of 400 pixels, a maximum width of 300 pixels, a minimum width of 300 pixels, and a minimum width of 250 pixels. This allows the images we load to still scale if they need to, but only within the limits we have provided.

    The default minimum height and width is 100 pixels and the default maximum height and width is 9999 pixels.

  5. Fancybox automatically resizes and repositions the Fancybox window when it is resized. Because we are showing images, we want it to stay the size it is no matter how the window resizes. We can use the autoResize and autoCenter settings to prevent or allow resizing when the window resizes, as shown in the following code:

    $('#show-fancybox').fancybox({
    autoResize: false,
    autoCenter: false
    });

    Now that we have set autoResize and autoCenter to false, the Fancybox pop up will not reposition or resize when the window is resized.

    The default value for both of these settings is !isTouch. Fancybox has a variable defined inside of it, which identifies touch devices, and prevents resizing and auto-centering on touch devices.

  6. The last setting to discuss is the fitToView setting. The fitToView setting will make the Fancybox pop up resize to fit inside the window when it is opened. Going along with the settings we have set in step 5, we probably don't want the window to resize when it is opened for the first time. We set fitToView to false, as shown in the following code:

    $('#show-fancybox').fancybox({
      autoResize: false,
      autoCenter: false,
      fitToView: false
    });
  7. Now when Fancybox is first opened, the window is the size we have set it to be, instead of attempting to resize it to fit the browser. The default value of the fitToView setting is true.

How it works...

When Fancybox first opens up, it identifies the size of the window and the size of the content it is loading. Next, it checks the settings we have provided to see how it needs to size the content to fit the parameters we have defined. After it has determined the appropriate height, width, margins, and padding to use for the Fancybox pop up, it applies them via the style attribute on the Fancybox pop up div.