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)

Loading additional types of content (Intermediate)


Fancybox can load more than just YouTube videos, images, and asynchronous content. The three content types we are going to cover in this recipe are SWF (flash files), inline, and iframe. We have already discussed the iframe content type to some extent when we loaded a YouTube video into Fancybox. However, iframes can contain more than just YouTube videos, so we will discuss some of the settings that are useful when using iframe content.

Getting ready

Before we start loading these different types of content, we'll need to find an SWF file. There is one included in the code examples; so if you need one, you can use that one. This is the file that I will be using in the instructions of this recipe. We will be creating a page with a link to each type of content in the order SWF, inline, and iframe.

How to do it...

  1. Add three links to the index.html file. We need one for the SWF file, one for inline content, and one for an iframe content, shown as follows:

    <a class="show-popup" data-fancybox-type="swf" href="demo.swf">Flash File(SWF)</a>
    <a class="show-popup" data-fancybox-type="inline" href="#inline">Inline Content</a>
    <a class="show-popup" data-fancybox-type="iframe" href="">iFrame Content</a>

    In the given code, we have an anchor tag for each content type. The first one is for the SWF file, which has the href attribute populated with the filename of the SWF file to use. The second is for the inline content, and the third is for the iframe content.

  2. Put the SWF file in the same directory as the index.html file. In the example code, the file is called demo.swf, but you can call it whatever you like.

  3. The scripts.js file should have a call to Fancybox on the show-popup class, shown as follows:

    $('.show-popup').fancybox();
  4. The Flash File link should now work with Fancybox. If you click on it, you should see your SWF file displayed in the Fancybox pop up. The demo.swf file included with the code examples is just the Fancybox Flash Demo text bouncing around a white stage, as shown in the following screenshot:

  5. Next up is the inline content. First, we need to add some inline content to the page that we want Fancybox to load. Let's just add some simple HTML:

    <div id="inline">
      <h2>Some Inline Content!</h2>
      <p>With some simple text as well!</p>
    </div>

    The HTML code we are adding is a div tag with an ID of inline and the div tag contains some basic HTML contents. This content can be added anywhere inside the body tag of the index.html file.

  6. Next we need to make sure that our link is correct. The href attribute of the inline link should be #inline. Note that inline is the ID of our div tag and is how Fancybox identifies the content it needs to grab from the HTML file. You should now see the contents of the div tag with the ID of inline displayed in Fancybox.

    Note

    Fancybox will automatically hide the inline HTML that it shows when the pop up is closed. In the There's more... section, we will cover how to prevent this from happening.

  7. Any external website can be loaded with the iframe option in Fancybox. For this example, we're going to load the Fancybox website at http://fancyapps.com/fancybox/.

  8. Our last link already has the data-fancybox-type attribute set to iframe, so we just have to add the Fancybox website URL to the href attribute of the anchor tag as follows:

    <a class="show-popup" data-fancybox-type="iframe" href="http://fancyapps.com/fancybox/">iFrame Content</a>

    You should see the Fancybox website loaded in the Fancybox pop up.

  9. All three links should continue to work on the page.

How it works...

Fancybox provides functionality for properly loading most of the common types of website content. For SWF files, it creates the appropriate HTML and uses the file provided in the href attribute. For inline content, it moves the content with the ID provided into the Fancybox div tag and replaces it with a placeholder string. Then, when Fancybox is closed, it knows where to put the content back on the page. For the iframe content, it relies on the functionality of iframes in web browsers to properly render the content from the URL provided in the href attribute of the link.

There's more...

There isn't much extra that Fancybox can do with these less common forms of content. However, some of the functionality regarding inline content may require some additional work to get the desired effect.

Showing inline content after closing Fancybox

As mentioned earlier, Fancybox likes to hide inline content once it has been shown in the Fancybox pop up. Fortunately, we can rely on one of the callback methods to make our content visible after Fancybox has closed. The method we will be using is afterClose. Let's make the inline content from step 5 display after Fancybox has been closed:

  1. We're going to expand on the inline functionality used earlier. Open the scripts.js file and add an object to the Fancybox call that has the afterClose setting.

  2. Define the afterClose setting as a function, which shows the content using the jQuery .show() method:

    $('.show-popup').fancybox({
      afterClose: function() {
        this.content.show();
      }
    });

    The this keyword in the afterClose function is the Fancybox object that is being used to show the content. It has a property called content, which is the content that the Fancybox pop up is configured to display. In this case, the content property is the div tag with an ID of inline. When we call the show() method on it, jQuery adds the display: block style to the div tag.

The inline content should now be displayed on the page when the Fancybox pop up is closed.