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 buttons to image slideshows (Simple)


Another helper that Fancybox provides is the slideshow buttons. The buttons allow the user to go back and forward, play and pause, and close the Fancybox pop up from a simple bar of buttons.

Getting ready

Like the thumbnails helper, we will need to include the JavaScript and stylesheet files for the buttons helper.

How to do it...

  1. Add the stylesheet and JavaScript tags to the head tag of the index.html file, shown as follows:

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

    Next, we need to add the buttons option to the settings for Fancybox:

    $('.fancybox').fancybox({
      helpers: {
        buttons: {}
      }
    });

    Here, we have set the helpers option with a property of buttons, which is an empty object. We have to provide an object for the buttons option otherwise it will not work, so we provide Fancybox with an empty object because we are not setting any property on buttons themselves.

  2. You should now see and be able to use the buttons at the top of the page.

How it works...

The buttons are HTML links that call some of the API methods provided by the Fancybox plugin when clicked.

There's more...

The buttons can have two settings defined for them, the tpl (template) to use to create them and the position property, which is used to position the buttons.

Changing the position of the buttons

The position setting allows us to tell the buttons whether to position them along the top or the bottom of the screen. By default, the buttons display along the top of the screen. If we set the position property of the buttons settings object to be bottom, then the buttons will be displayed along the bottom of the screen.

$('.fancybox').fancybox({
  helpers: {
    buttons: {
      position: 'bottom'
    }
  }
});

Here, we have set the position property of the buttons settings object to be bottom, so our buttons will be displayed along the bottom of the screen.

Adjusting the HTML template

The Fancybox buttons helper allows us to provide an HTML template to render the buttons. The default buttons use a template as in the following code:

<div id="fancybox-buttons" class="bottom special">
<ul>
<li><a class="btnPrev" title="Previous" 
  href="javascript:;"></a></li>
<li><a class="btnPlay" title="Start slideshow" 
  href="javascript:;"></a></li>
<li><a class="btnNext" title="Next" href="javascript:;"></a></li>
<li><a class="btnToggle" title="Toggle size" 
  href="javascript:;"></a></li>
<li><a class="btnClose" title="Close" 
  onclick="jQuery.fancybox.close()"></a></li>
</ul>
</div>

I would recommend starting with the default button template and modifying it if you want to adjust the template at all. I would also recommend changing the template by using an HTML element on the page using the following steps:

  1. Create the template on the page inside the index.html file, shown as follows:

    <div class="hidden">
    <div id="fancybox-buttons" class="bottom 
      special"><ul><li><a class="btnPrev" title="Previous" 
      href="javascript:;"></a></li><li><a class="btnPlay" 
      title="Start slideshow" 
      href="javascript:;"></a></li><li><a class="btnNext" 
      title="Next" href="javascript:;"></a></li><li><a 
      class="btnToggle" title="Toggle size" 
      href="javascript:;"></a></li><li><a class="btnClose" 
      title="Close" 
      href="javascript:jQuery.fancybox.close();"></a>
      </li></ul></div>
    </div>
    

    Note that in the example, we placed the fancybox-buttons div inside the div tag with the hidden class, which makes it such that the buttons are not displayed on the page until Fancybox is displayed.

  2. Retrieve the element from the page via jQuery and set it into the tpl setting, shown as follows:

    $('.fancybox').fancybox({
      helpers: {
        buttons: {
          position: 'bottom',
          tpl: $('#fancybox-buttons')
        }
      }
    });

    In the previous code, we have set the tpl property of the buttons settings object to be the entire fancybox-buttons div, which we have retrieved from the HTML document via jQuery.