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)

Changing animation effects (Intermediate)


Fancybox allows us to change all of the settings for the various animations it does. Let's talk about changing these settings and its impact on the Fancybox pop up.

Getting ready

We're going to continue to use the gallery we used in the previous recipe. Fancybox uses the jQuery animation effects to produce its animations. This means that all of the animations have three basic pieces: effect, easing, and duration. The effect is the name of the animation effect that we want Fancybox to use. Easing is the curve that represents the speed throughout the duration of the animation. Duration is the amount of time the animation will take to complete. We will look at changing all of these settings for Fancybox. To learn more about jQuery animation check out the jQuery API at http://api.jquery.com/animate/.

How to do it...

  1. Let's make our Fancybox take a whole second to display and close. We can do this using the openSpeed and closeSpeed settings. All of the animation durations are in milliseconds.

    $('.fancybox').fancybox({
      openSpeed: 1000,
      closeSpeed: 1000
    });

    Now our Fancybox pop up will take one second (1000 milliseconds) to open and close. The default for these settings is 250 milliseconds.

  2. We can also set the speed that it takes to switch between images using the nextSpeed and prevSpeed settings. Let's make this transition also take 1000 milliseconds, as shown in the following code:

    $('.fancybox').fancybox({
      openSpeed: 1000,
      closeSpeed: 1000,
      nextSpeed: 1000,
      prevSpeed: 1000
    });

    Now all of our animations will take an entire second to run. The default for these settings is also 250 milliseconds.

  3. Next let's look at changing the effect that Fancybox uses to open and close. To change the effect, we use the openEffect and closeEffect settings. We have three options to select from: fade, elastic, and none. The default option is fade, which makes Fancybox fade in and out when it is opened and closed. That's pretty boring, so let's use elastic instead.

    $('.fancybox').fancybox({
      openSpeed: 1000,
      closeSpeed: 1000,
      nextSpeed: 1000,
      prevSpeed: 1000,
      openEffect: 'elastic',
      closeEffect: 'elastic'
    });

    Now the Fancybox pop up starts small and expands. We can also set the effect to none, which turns the animation off.

  4. We can also apply these same effects to the animations between images in the slideshow. Let's make the transition between images just fade in and out. By default, Fancybox uses elastic for the image transitions. We use the nextEffect and prevEffect settings to change the effect of the next and previous image animations, as shown in the following code:

    $('.fancybox').fancybox({
      openSpeed: 1000,
      closeSpeed: 1000,
      nextSpeed: 1000,
      prevSpeed: 1000,
      openEffect: 'elastic',
      closeEffect: 'elastic',
      nextEffect: 'fade',
      prevEffect: 'fade'
    });

    Now the transition between images in the slideshow will just be a simple fade in and out. You can also set these settings to none, which will just switch the images without any animation.

  5. The last setting to change is easing. We can change easing for the different animations in the exact same way as the previous two settings. We can change the open and close easing using openEasing and closeEasing. We can change the next and previous easing using nextEasing and prevEasing. Let's change these to use the linear easing curve, as shown in the following code:

    $('.fancybox').fancybox({
      openSpeed: 1000,
      closeSpeed: 1000,
      nextSpeed: 1000,
      prevSpeed: 1000,
      openEffect: 'elastic',
      closeEffect: 'elastic',
      nextEffect: 'fade',
      prevEffect: 'fade',
      openEasing: 'linear',
      closeEasing: 'linear',
      nextEasing: 'linear',
      prevEasing: 'linear'
    });

    Now our animation will have no easing. Using the linear easing curve means that our animation speed will be constant throughout the entire duration of the animation. These two, linear and swing, are the only two easing curves available in standard jQuery.

How it works...

Animations are complex beasts and the animations available as part of Fancybox can be customized quite well. Speed and effect are fairly straightforward; the more difficult concept is easing. If you are unsure about adjusting easing, I would recommend against changing this setting. The default option of swing is a very natural easing curve (slow to fast to slow is exactly how most things move in real life and is what the swing curve does). If you are looking for more easing curves, I would recommend looking at jQuery UI at http://jqueryui.com/, which introduces a handful of additional easing curves. The default easing curve for all Fancybox effects is swing.