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)

Manipulating image slideshows (Advanced)


Fancybox also has API methods for interacting with image slideshows. We can use these methods to start or stop playback, move to the next or previous image, and move to any image in the slideshow. Let's create some simple links to move around the slideshow using the API.

Getting ready

We'll start with code similar to that found in the Creating an image gallery recipe. The only difference will be that we will include the styles.css file so we can add some custom styles.

How to do it...

  1. Let's create the HTML and CSS for the custom buttons we are going to have. We'll start with the following HTML code:

    <div class="custom-controls">
      <a href="#" class="previous">Previous</a>
      <a href="#" class="play-pause">Play</a>
      <a href="#" class="next">Next</a>
      <a href="#" class="random">Random</a>
    </div>

    The HTML we have is a div element containing the four links we will use to trigger the API functions.

  2. Next let's add some styles to the styles.css file to make the custom-controls div show all the time:

    h1 {
      margin-top: 50px;
    }
    
    .custom-controls {
      background: white;
      height: 50px;
      line-height: 50px;
      width: 93%;
      position: fixed;
      top: 0px;
      z-index: 9999;
      text-align: center;
      
    }
    
    .custom-controls a {
      padding: 0 15px;
    }

    Note that the links will be visible even when Fancybox is not open. These controls will still work even without Fancybox being displayed. For the sake of brevity, we have not made the controls hide and show with the Fancybox pop up, but I would recommend that you do. With these styles in the styles.css file, the custom-controls div should look like the following screenshot:

  3. Now that we have the HTML and CSS files in place, we can start making our links work. Let's start with the Previous link, which will move the slideshow back by one image. First we need to bind to the click event. Inside that event binding, we just need to call $.fancybox.prev() to move Fancybox to the previous image, as shown in the following code:

    $('.previous').click(function() {
      $.fancybox.prev();
    });
  4. Next let's make the Next link work. This will be exactly the same as the Previous link, except we will call $.fancybox.next(), as shown in the following code:

    $('.next').click(function() {
      $.fancybox.next();
    });
  5. The play and pause functionality is also just as simple, but we're going to make the text change on the link as well. First let's set up the functionality, as shown in the following code:

    $('.play-pause').click(function() {
      $.fancybox.play();
    });

    The link will now play and pause the slideshow, depending on whether or not the slideshow is currently playing. Let's make the button text reflect the action we will be taking, instead of just always saying Play. The following code should be used, instead of the previous code:

    $('.play-pause').click(function() {
      $.fancybox.play();
      if ($(this).text() === 'Play') {
        $(this).text("Pause");
      } else {
        $(this).text("Play");
      }
    });

    Note

    For the sake of simplicity, the Play/Pause button just checks its own text to reflect its current state. On a real website, it would be better to listen for the Fancybox events, onPlayStart and onPlayEnd, and update the text when those are triggered. Events are discussed in the next recipe.

    In the previous code, we are still calling the Fancybox API function. We are also checking the text of the play-pause link. If the text is Play then we set the text to Pause. If it is anything other than Play, we set it to Play. Now our link represents the state of the Fancybox slideshow.

  6. The last link is our random link. This link will use the Fancybox API $.fancybox.jumpto method, which moves the slideshow to the image at the provided index. We will be providing a random number between zero and four as the index. First we need to create our random number. JavaScript has a built-in way to get random numbers using the Math.random() function. We will use this to get our random number. We will then provide this random number to the $.fancybox.jumpto() function, as shown in the following code:

    $('.random').click(function() {
      var randomNumber = Math.floor(Math.random() * 4);
    
      $.fancybox.jumpto(randomNumber);
    });

    In the previous code, the randomNumber variable is a random integer in the range of zero to three (including three). We provide that to the $.fancybox.jumpto() function and when the Random link is clicked on, a random image will display. Note that with this implementation, the image that is being transitioned to may be the same as the one that is currently being displayed.

How it works...

In all of the previous examples, we rely on the jQuery click event binding functionality to bind our click events. All of the bindings are passed an anonymous function, which has our code inside it. However, in the case of the Next and Previous links, we could simply provide the API function directly without using an anonymous function, as shown in the following code:

$('.previous').click($.fancybox.prev);

I decided to make all of the click events use anonymous functions to make all of these events consistent to avoid any confusion about differences between the API functions.

The Math.random() function used to generate a random number creates a random number between zero and one. In the previous example, we multiplied it before hand, because we are then left with a number between zero and 3.9999999. Next, we passed this number to the Math.floor function, which rounds the number down to the nearest whole number.