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.
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/.
Let's make our Fancybox take a whole second to display and close. We can do this using the
openSpeed
andcloseSpeed
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.
We can also set the speed that it takes to switch between images using the
nextSpeed
andprevSpeed
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.
Next let's look at changing the effect that Fancybox uses to open and close. To change the effect, we use the
openEffect
andcloseEffect
settings. We have three options to select from:fade
,elastic
, andnone
. The default option isfade
, which makes Fancybox fade in and out when it is opened and closed. That's pretty boring, so let's useelastic
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.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 thenextEffect
andprevEffect
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.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
andcloseEasing
. We can change the next and previous easing usingnextEasing
andprevEasing
. Let's change these to use thelinear
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
andswing
, are the only two easing curves available in standard jQuery.
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
.