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.
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.
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.Next let's add some styles to the
styles.css
file to make thecustom-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, thecustom-controls
div should look like the following screenshot: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(); });
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(); });
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
andonPlayEnd
, 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 isPlay
then we set the text toPause
. If it is anything other thanPlay
, we set it toPlay
. Now our link represents the state of the Fancybox slideshow.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 theMath.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.
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.