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)

Installing Fancybox (Simple)


We're going to start by creating a basic HTML page, which will have jQuery and the Fancybox plugin loaded on it. We'll walk through the steps to get the plugin and make sure that it is loading on the page.

Getting ready

To install the Fancybox plugin, you will need only two things: a text editor or some way to create HTML, CSS, and JavaScript files, and an Internet connection to obtain the plugin.

How to do it...

  1. Go to the Fancyapps website, http://fancyapps.com/fancybox/.

  2. Scroll down to the download link and download the files.

  3. Create a folder that will be the location of our demo HTML page.

  4. Extract the zip file that was just downloaded and move the folder to the directory that will be used for the page. Rename the folder to fancybox.

  5. Create a basic HTML page inside the folder. Here's a sample of a really basic HTML5 page with a title:

    <!DOCTYPE html>
    <html>
    <head>
        <title>Chapter 1</title>
    </head>
    <body>
        <h1>Installing Fancybox</h1>
    </body>
    </html>

    Tip

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

  6. Add the following script tags to load jQuery and Fancybox just under the <title> tag:

    <link rel="stylesheet" 
      href="fancybox/source/jquery.fancybox.css?v=2.1.4" 
      type="text/css" media="screen" />
    <script type="text/javascript" 
      src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/
        jquery.min.js"></script>
    <script type="text/javascript" 
      src="fancybox/source/jquery.fancybox.pack.js?v=2.1.4">
    </script>
    

    First, we load CSS for Fancybox, then jQuery (via Google CDN), and then the main Fancybox JavaScript file.

  7. Now that we have everything loaded on the page, let's load the page and check for any JavaScript errors. If everything has worked, there should be no errors on the page and you should be able to open the console and type $.fancybox. You will see something like the following code if everything has worked:

    function (){b.open.apply(this,arguments)}

    If you see that undefined Fancybox is not loaded on the page, check the file paths that you have in the src attribute of your script tags. Remember that we changed the directory to be called fancybox in step four.

How it works...

We now have a basic HTML page, which has jQuery and the Fancybox plugin loaded. The download from the Fancybox website includes the full source of the plugin in a non-minified form, the minified version (which we are currently loading on the page), the necessary images and CSS files, and a folder that includes some demos.

We are loading jQuery from the Google CDN for several major reasons:

  • By using the Google CDN, we don't have to pay to host the file when we are using Fancybox on a live website.

  • Since it is common for other websites to use the Google CDN for jQuery, it is likely that users coming to our website will have the file cached. This makes our website load faster!

  • Using jQuery from the Google CDN also allows us to easily update the version of jQuery that is being used.

    Note

    In the previous example code, we are using jQuery version 2.0, which does not support IE 6, 7, and 8. If you need to support any of these browsers, use an older version of jQuery such as 1.9.2. Fancybox works with both versions.

    Because we are using jQuery from the Google CDN, it will only work when the computer has access to the Internet. If you want to do any work while not having access to the Internet, you will need to download a copy of jQuery onto your computer.

If you looked around on the Fancybox website, you may have noticed that all of the files that are considered part of Fancybox are not included. For the very basic use of Fancybox, these other files are not necessary. These other files add extra functionalities to Fancybox, but Fancybox works just fine without them. Later in this book, we will discuss what extra functionalities these files provide.