Follow these steps to set up a CrossSlide slideshow:
<div id="slideshow"> <img src="images/600/AguaAzul.jpg" alt="Agua Azul"/> </div>
I'm going to simply show the first photo from the slideshow for users with JavaScript disabled. I've given my container <div>
an id
of slideshow.
styles.css
and add some CSS to define the width and height of the slideshow:#slideshow { width:600px;height:400px; }
You'll find the Download minified link near the top of the page. The rest of the page shows several examples of the CrossSlide plugin in action. Take a look through the examples. You'll see that it can do everything from a simple crossfade slideshow similar to what we built in the first section of this chapter to a fully animated panning and zooming slideshow.
Now that you've had a look at some of the types of slideshows you can create with the CrossSlide plugin, here are a few things to keep in mind:
jquery.cross-slide.min.js
and save the file in your scripts
folder. scripts.js:
<script src="scripts/jquery.js"></script> <script src="scripts/jquery.cross-slide.min.js"></script> <script src="scripts/scripts.js"></script> </body> </html>
scripts.js
file and we'll get started with the CrossSlide plugin by selecting our slideshow container and calling the crossSlide()
method:var slideshow = $('#slideshow'), slideshow.crossSlide();
Recall that a variable is just a container for something. In this case, we've selected
the slideshow container and placed it in a variable called slideshow
. We've done
this because we're going to reference the container several times in our script. By
saving the slideshow container in a variable, we're preventing jQuery from having to
query the DOM looking for the slideshow container each time we want to refer to it,
making our code more efficient.
crossSlide()
method appears to have had no effect on our page. You'll still see the placeholder content inside our slideshow container and there's no slideshow happening. That's because we have to pass not only settings to the crossSlide()
method, but also the list of photos we'd like to show in our slideshow. Inside the crossSlide()
method's parentheses, insert a pair of curly brackets and we'll pass in a key/value pair to configure the length of time the fade between photos will take in seconds:var slideshow = $('#slideshow'), slideshow.crossSlide({ fade: 1 });
Note that we're expressing the length of time in seconds, not milliseconds. The CrossSlide plugin is set up to expect seconds as units of time rather than the milliseconds that we usually find in JavaScript.
crossSlide()
method. An array is wrapped in square brackets:slideshow.crossSlide({ fade: 1 }, [ //Our list of photos will go here. ] );
src
key:slideshow.crossSlide({ fade: 1 }, [ { src: 'images/1000/AguaAzul.jpg' } ] );
slideshow.crossSlide({ fade: 1 }, [ { src: 'images/1000/AguaAzul.jpg', alt: 'Agua Azul, Mexico' } ] );
from
and to
keys:slideshow.crossSlide({ fade: 1 }, [ { src: 'images/1000/AguaAzul.jpg', alt: 'Agua Azul, Mexico', from: 'top left 1x', to: 'bottom right .8x' } ] );
slideshow.crossSlide({ fade: 1 }, [ { src: 'images/1000/AguaAzul.jpg', alt: 'Agua Azul, Mexico', from: 'top left 1x', to: 'bottom right .8x', time: 4 } ] );
slideshow.crossSlide({ fade: 1 }, [ { src: 'images/1000/AguaAzul.jpg', alt: 'Agua Azul, Mexico', from: 'top left 1x', to: 'bottom right .8x', time: 4 }, { src: 'images/1000/BurneyFalls.jpg', alt: 'Burney Falls, California, USA', from: 'top left 1.2x', to: 'bottom right .8x', time: 5 }, { src: 'images/1000/Cachoeira_do_Pacheco.jpg', alt: 'Cachoeira do Pacheco, Venezuela', from: '50% 0% 1.2x', to: '50% 60% .6x', time: 4 }, { src: 'images/1000/Deer_Leap_Falls.jpg', alt: 'Deer Leep Falls, Pennsylvania, USA', from: '50% 50% 1.2x', to: '50% 100% .8x', time: 3 } ] );
Note that I can choose how long each photo displays — allowing a particularly stunning photo to linger on the page longer if I choose, or moving a smaller or less interesting photo off the page more quickly.
Now if you refresh the page in the browser, you'll see a panning and zooming slideshow of your photos. We're getting closer!
crossSlide()
method for each photo to create a caption. First, I'm going to go back to my HTML markup and add a container for the caption. You can style this with CSS however you'd like:<div id="slideshow"> <img src="images/600/AguaAzul.jpg" alt="Agua Azul"/> </div> <div class="caption"></div>
Keep in mind that the container for your caption has to appear outside of the slideshow container. If you place it inside, it will be removed when the CrossSlide plugin replaces the slideshow container's content with the slideshow.
Now, we've got a place to display our caption, so we just need a way to put our
captions into that container. The crossSlide()
method will accept a callback
method along with our settings and array of images. This callback function will be
called each time an image starts to crossfade into the next image, and it is called
again when the fade is complete.
slideshow.crossSlide({ fade: 1 }, [ { src: 'images/1000/AguaAzul.jpg', alt: 'Agua Azul, Mexico', from: 'top left 1x', to: 'bottom right .8x', time: 4 }, { src: 'images/1000/BurneyFalls.jpg', alt: 'Burney Falls, California, USA', from: 'top left 1.2x', to: 'bottom right .8x', time: 4 }, { src: 'images/1000/Cachoeira_do_Pacheco.jpg', alt: 'Cachoeira do Pacheco, Venezuela', from: '50% 0% 1.2x', to: '50% 60% .6x', time: 4 }, { src: 'images/1000/Deer_Leap_Falls.jpg', alt: 'Deer Leep Falls, Pennsylvania, USA', from: '50% 50% 1.2x', to: '50% 100% .8x', time: 3 } ], function(index, img, indexOut, imgOut) { //our callback function goes here } );
Our callback function is passed four possible values: the index of the current image, the current image itself, the index of the previous image, and the previous image itself. The index of the image is simply its place in the slideshow by number. JavaScript, like other programming languages, starts counting at 0 instead of 1. So the index of the first image in the slideshow is 0, the second image's index is 1, and so on.
Remember I said the callback function is called once when the crossfade starts and once again after the crossfade is finished? If the crossfade is starting, the callback function will get all four values—the index of and the current image, and the index of and the previous image. If the crossfade is finished, we'll only get two values: the index of the current image and the current image itself.
], function(index, img, indexOut, imgOut) { var caption = $('div.caption'), if (indexOut == undefined) { caption.text(img.alt).fadeIn(); } else { caption.fadeOut(); } }
If the crossfade is finished, then indexOut
will be undefined
, since there won't
be a value for that variable passed to the callback function. It's easy to check if
that value is undefined to figure out if the crossfade animation is starting or
finishing. Then, we use jQuery's text()
method to set the text of the caption to
the alt
value we included with each image and fade the caption in. If the crossfade
animation is just starting on the other hand, we'll just fade the caption out.
Now if you refresh the page in the browser, you'll see that the caption fades in with each photo and fades out as the crossfade is starting. It's a nice smooth transition from one caption to the next.
variant
to true:
slideshow.crossSlide({ fade: 1, variant: true }, [ { src: 'images/1000/AguaAzul.jpg', ...
This will change your slideshow so that each photo will complete panning and zooming before starting the crossfade to the next photo.
What just happened?
Don't be worried if your head is spinning — the CrossSlide plugin is by far the most developer-y plugin we've used yet. Although this plugin isn't super designer-friendly, I hope you can see that even this type of plugin is within your reach if you have a little patience and are willing to experiment a bit. Carefully studying the code for examples will take you pretty far.
We set up a container which held our static content for users with JavaScript disabled. Then we set up the CrossSlide plugin to replace that content with a dynamic panning and zooming slideshow for the users with JavaScript enabled. We set the length of the crossfade to 1 second, and then passed in our array of images, including the URL, caption, animation starting point, animation ending point, and duration for each image. Finally, we took advantage of the callback function provided by the CrossSlide plugin to fade in each photo's caption and fade it back out when the photo itself starts to fade out. We also took a look at how to make the slideshow a bit less CPU-intensive for those situations where it might cause problems.
3.137.218.215