Fancybox provides a number of methods that we can use to programmatically interact with the Fancybox pop up. Let's take a look at some of these methods and at what they allow us to do.
To make things simple, we are just going to use a single-image pop up like the one found in the Installing Fancybox recipe. We'll be looking at the pieces of the API that allow us to interact with just the Fancybox pop up in this recipe. We'll cover manually opening and closing Fancybox, the update and toggle functions, the reposition function, the show and hide loader functions, and the close function.
Let's start with opening the Fancybox pop up using the API. We used this briefly in the recipe on changing slideshow settings. The method is
$.fancybox.open
and can be passed a range of different parameters. The first parameter is the content to open inside of Fancybox and the second parameter is the options we want to use for this Fancybox pop up. If we want to open Fancybox using the API method and have it do the exact same thing as in the Fancybox with a single image recipe, we'll call Fancybox and pass it thehref
tag of the image as shown in the following code:$('#show-fancybox').click(function() { $.fancybox.open('images/waterfall.png'); });
The previous code uses a jQuery
click
event binding, which is passed an anonymous function, to tell Fancybox to open and use the string we provided to open. The string can be a range of different types of content that Fancybox can open. If you wanted it to open an image gallery, images can be passed in as an array, as shown in the code:$('#show-fancybox').click(function() { $.fancybox.open([ {href:'images/waterfall.png'}, {href:'images/frozen-lake.png'} ]); });
The objects can contain the
href
andtitle
tags for the image, as shown in the following code:{href:'images/frozen-lake.png', title: "Frozen Lake"}
For the remainder of this recipe, we will have Fancybox load some HTML. Add this HTML to the
index.html
file inside the body:<div id="fancybox-content" class="hidden"> <h2>Super Simple Fancybox Popup</h2> <p>With some basic text for content.</p> <a href="#" class="close">Close Fancybox</a> </div>
We also need to slightly change the link to open Fancybox in the
index.html
file:<a id="show-fancybox" href="#">Show The HTML</a>
Lastly, we need to change our open function for Fancybox inside the
scripts.js
file:$('#show-fancybox').click(function() { $.fancybox.open($('#fancybox-content')); });
Now our Fancybox
open
method contains a jQuery object for the content to display. When you open Fancybox it should look like the following screenshot:Now that we have Fancybox opening our HTML, let's look at using the
$.fancybox.close
function. This function closes the Fancybox pop up. Let's make the Close Fancybox link we have inside the Fancybox pop up, close the pop up.$('.close').on('click', function() { $.fancybox.close(); });
Fancybox has other functions that are advantageous when you have dynamically changing content inside the Fancybox pop up. If you want to tell Fancybox to reposition the location of the pop up, it can be done with a call to the
$.fancybox.reposition()
function. Fancybox will move the pop up to the appropriate location inside the window.Fancybox also provides us with a function to resize the Fancybox window as required. The
$.fancybox.update()
function tells Fancybox to resize the height of the Fancybox pop up to match the content. It is necessary when you are programmatically changing the content inside the Fancybox pop up.The
$.fancybox.toggle()
function will put Fancybox into fullscreen mode, if necessary. It will only have an effect if the content is larger than the current Fancybox pop-up window.You can show and hide the Fancybox loader using the
$.fancybox.showLoading()
and$.fancybox.hideLoading()
functions. These functions are useful when you are processing data inside the Fancybox pop up and want to tell the user that your content or information is loading.The last major function available to us is the
$.fancybox.cancel()
function. This function will stop loading the Fancybox pop up. It may be useful when the content takes too long to load and you want to just stop (especially if the content is an iframe and the website you are trying to load is not available).
The Fancybox API is simply a set of functions that the plugin allows us to call. These functions are exposed to make it easier for us to work with the plugin and have increased control over how the plugin works.
You may have noted that when we created the close link we bound to the click
event using the jQuery .on()
function. This is because when Fancybox moves the HTML into the Fancybox pop up, the events that have been bound to the objects are reset. By using the .on()
function, the event is listened for at the window level (similar to the jQuery .live()
function); so no matter where a link with the class of close exists, the event binding will trigger. For more information on event binding and delegation using jQuery, see the jQuery API at http://api.jquery.com/on/.