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)

Adding thumbnails to a gallery (Simple)


Fancybox provides the ability to have a thumbnails display under an image gallery. Let's take a look at getting the thumbnails displayed under our image gallery.

Getting ready

We're going to use the image gallery that we already created in the Creating an image gallery recipe. If you don't have yours available, you can find a simple image gallery in the example code.

How to do it...

  1. Remember all of those extra files that come with the Fancybox download? We need to add the stylesheet and JavaScript files necessary for the thumbnails to work. In the head tag of our index.html file, add the following two lines:

    <link rel="stylesheet" 
      href="fancybox/source/helpers/jquery.fancybox-
      thumbs.css?v=1.0.7" type="text/css" media="screen" />
    <script type="text/javascript" 
      src="fancybox/source/helpers/jquery.fancybox-
      thumbs.js?v=1.0.7"></script>
    

    We have now loaded the necessary files for the thumbnails helper. Helpers are supplemental functionality to the Fancybox plugin. They aren't required for the plugin to work but they can provide some great additional functionalities.

  2. Next, we need to tell Fancybox to use the thumbnails helper. We do that by passing the Fancybox call a setting as follows:

    $('.fancybox').fancybox({
      helpers: {
        thumbs: {
          width: 75,
          height: 50
        }
      }
    });
  3. In the given code, we are calling Fancybox with a helpers setting, which has a thumbs property that has two properties: width and height. The width and height properties define the width and height of the displayed thumbnails. You should now see the thumbnails along the bottom of your window as shown in the following screenshot:

How it works...

The thumbnails helper creates image tags along the bottom of the screen, which contain the same images that will be displayed as part of the gallery. It simply constrains the height and width of the images using CSS. When you click on an image, the helper takes you to the gallery of that image and adjusts the positioning of the thumbnails.

There's more...

The thumbnails helper has a few settings that we can use to adjust its positioning as well as the way it determines the source of the thumbnail images. We're also going to take a look at applying styles to the thumbnails to adjust their positioning in the window.

Changing the thumbnail source

The thumbnail sources are, by default, the exact same images as the large ones. By using the exact same images, the load times of the thumbnails will be significantly greater and the thumbnails will require considerably more bandwidth. Let's take a look at adjusting the location of our thumbnails. We can use the source setting of the thumbnails helper to provide a function, which provides the URL for each image's thumbnail. Let's change the source of the thumbnails for the image gallery:

  1. In the index.html file, add the data-thumbnail attribute to each image. The value should be the thumbnail you want to use for the image, shown as follows:

    <a class="fancybox" rel="gallery" data-thumbnail="images/frozen-lake-sm.png" href="images/frozen-lake.png">Frozen Lake</a>
  2. Add the source property to the thumbnails settings object:

    source: function(image) {
      return $(image.element).data("thumbnail");
    }
  3. Have the function return the URL that matches this image's thumbnail. In this case, it should return the data-thumbnail attribute's value, which we get by using the jQuery data method and telling it that we want to get the thumbnail data item, or the data-thumbnail attribute, shown as follows:

    $('.fancybox').fancybox({
      helpers: {
        thumbs: {
          width: 75,
          height: 50,
          source: function(image) {
            return $(image.element).data("thumbnail");
          }
        }
      }
    });

    The call to Fancybox should now look something like the given code, with all of our settings being passed in.

Positioning the thumbnails

The thumbnails can be positioned to either the top or the bottom of the screen. The default is to have the thumbnails at the bottom of the screen, but if you want to make them visible at the top of the screen, simply add the position property to the thumbs settings object, shown as follows:

$('.fancybox').fancybox({
  helpers: {
    thumbs: {
      width: 75,
      height: 50,
      source: function(image) {
        return $(image.element).data("thumbnail");
      },
      position: 'top'
    }
  }
});

With the position: 'top' setting applied, you should see the thumbnails along the top of the window.

Showing hidden content

What if we want the inline content to only show up in the Fancybox pop up? This can be done with simple CSS. All we have to do is make the div tag with the ID of inline have a style tag of display: none. In the head tag at the top of the index.html page, add a style block as follows:

<style type="text/css">
  #inline {
    display: none;
  }
</style>

This style block applies the display: none CSS to any element with an ID of inline. We also need to undo the function that we defined in the earlier section, so make sure that the Fancybox call on the show-popup links is back to:

$('.show-popup').fancybox();

Now, the content should be hidden when the page loads, visible in the Fancybox pop up, and hidden once Fancybox closes.