The jCarousel plugin has taken care of setting up the carousel for us, but we want to get fancy and also add a slideshow area.
function slideshowInit() { // Slideshow setup goes here } $(document).ready(function(){ slideshowInit(); $('#thumb-carousel').jcarousel({ scroll: 6, wrap: 'circular' }); });
jcarousel()
method as follows:var thumbs = $('#thumb-carousel'), function slideshowInit() { // Slideshow setup goes here } $(document).ready(function(){ slideshowInit(); thumbs.jcarousel({ scroll: 6, wrap: 'circular' }); });
slideshowInit()
function, we'll call jQuery's wrap()
method to wrap the list in a<div>
.function slideshowInit() {
thumbs.wrap('<div id="stage-wrap"></div>'),
}
prepend()
method so that these elements are inserted into stage-wrap div
before the thumbs list.function slideshowInit() {
thumbs.wrap('<div id="stage-wrap"></div>'),
$('#stage-wrap').prepend('<div id="slideshow-next"></div><div id="slideshow-prev"></div><div id="stage"></div>'),
}
styles.css
file and add some styles for these new elements as follows:#stage-wrap { position:relative;width:856px; } #stage { width:600px;height:400px;padding:0 0 20px 0;position:relative;text-align:center;margin:0 128px; } #stage img { position:absolute;top:0;left:50%;margin-left:-300px; } #slideshow-next { position:absolute;right:80px;top:160px;width:38px;height:75px;cursor:pointer;background:transparent url(images/arrow-right.png) no-repeat 0 0; } #slideshow-next:hover, #slideshow-next:active { background-position:0 -75px; } #slideshow-prev { position:absolute;left:80px;top:160px;width:38px;height:75px;cursor:pointer;background:transparent url(images/arrow-left.png) no-repeat 0 0; } #slideshow-prev:hover, #slideshow-prev:active { background-position:0 -75px; }
All of our full-size images are the same size, 600x400, so we can set that as the width and height of the stage and position the next and previous image buttons accordingly. If you view the page in a browser now, you should see a large blank area left for the stage and the next and previous image buttons on either side of it, all positioned above the thumbnail carousel.
opacity
of the stage to 0
as shown in the following code:function slideshowInit() { thumbs.wrap('<div id="stage-wrap"></div>'), $('#stage-wrap').prepend('<div id="slideshow-next"></div><div id="slideshow-prev"></div><div id="stage"></div>'), var stage = $('#stage'), stage.css('opacity',0); }
We've hidden the stage from view so that we can load the images into it without the site visitor seeing the images loading. This lets us have some control over how the slideshow appears as it's being created. We're going to keep the stage invisible until there's something to see.
function slideshowInit() {
thumbs.wrap('<div id="stage-wrap"></div>'),
$('#stage-wrap').prepend('<div id="slideshow-next"></div><div id="slideshow-prev"></div><div id="stage"></div>'),
var stage = $('#stage'),
stage.css('opacity',0);
var imageLinks = thumbs.find('a'),
var src;
}
The links to the full-size images are contained in the thumbnail list, which we can
refer to with the thumbs
variable. We're just finding all of the links in that list and
storing them in a variable called imageLinks
. Next, we're setting up an empty
container called src
where we're going to store the url for the images. Though for
now, we're leaving that container empty. We'll fill it up in a moment.
each()
method to loop through each link and create an image.function slideshowInit() {
thumbs.wrap('<div id="stage-wrap"></div>'),
$('#stage-wrap').prepend('<div id="slideshow-next"></div><div id="slideshow-prev"></div><div id="stage"></div>'),
var stage = $('#stage'),
stage.css('opacity',0);
var imageLinks = thumbs.find('a'),
var src;
imageLinks.each(function(i) {
// We'll create our images here
});
}
This is the jQuery way of saying For each link, do this thing.
src
attribute of the image is going to equal the href
attribute of the link. In other words, a link as follows:<a href="images/600/Switzerland.jpg">Switzerland</a>
will be used to create an image as follows:
<img src="images/600/Switzerland.jpg"/>
So the first thing we'll do is get that empty src
variable we created earlier and store
the URL for the image in it:
imageLinks.each(function(i) {
src = $(this).attr('href'),
});
Next, we're going to create an image with this src
attribute. I'm going to store my
newly created image in a variable called img:
imageLinks.each(function(i) {
src = $(this).attr('href'),
var img = $('<img/>', {
src: src,
css: {
display: 'none'
}
});
});
We've set the display of the image to none, to hide all of the images created in this
way. We've set the src
attribute of the image to the src
variable that's holding the
URL of the image.
imageLinks.each(function(i) {
src = $(this).attr('href'),
var img = $('<img/>', {
src: src,
css: {
display: 'none'
}
});
img.appendTo(stage);
});
jQuery's appendTo()
method lets us append the image to the stage.
function slideshowInit() {
thumbs.wrap('<div id="stage-wrap"></div>'),
$('#stage-wrap').prepend('<div id="slideshow-next"></div><div id="slideshow-prev"></div><div id="stage"></div>'),
var stage = $('#stage'),
stage.css('opacity',0);
var imageLinks = thumbs.find('a'),
var src;
imageLinks.each(function(i) {
src = $(this).attr('href'),
var img = $('<img/>', {
src: src,
css: {
display: 'none'
}
});
img.appendTo(stage);
});
stage.css('opacity',1);
}
data()
method to store an index number in each thumbnail link. I'll then use that index to find and show the appropriate image.Basically, we're going to number the links in the list. You'd think they'd be numbered 1 through 12, but remember that JavaScript counting starts at 0, so the thumbnail images will be numbered 0 through 11. When a thumbnail is clicked, we'll get the index number of that thumbnail, find the image on the stage with that same index and show it. So if our site visitor clicks thumbnail number 6, we'll find image number 6 on the stage and show it.
First up, we have to assign the index numbers to the thumbnails. Inside the document ready statement, add a small function to loop through each thumbnail and add an index number as follows:
$(document).ready(function(){
thumbs.find('a').each(function(index){
$(this).data('index', (index));
});
slideshowInit();
thumbs.jcarousel({
scroll: 6,
wrap: 'circular',
initCallback: nextPrev
});
});
slideshowInit()
function, we'll bind our function to the click event:function slideshowInit() {
thumbs.wrap('<div id="stage-wrap"></div>'),
$('#stage-wrap').prepend('<div id="slideshow-next"></div><div id="slideshow-prev"></div><div id="stage"></div>'),
var stage = $('#stage'),
stage.css('opacity',0);
var imageLinks = thumbs.find('a'),
var src;
imageLinks.each(function(i) {
src = $(this).attr('href'),
var img = $('<img/>', {
src: src,
css: {
display: 'none'
}
});
img.appendTo(stage);
});
stage.css('opacity',1);
imageLinks.bind('click', function(){
// Function to find and show an image goes here
});
}
imageLinks.bind('click', function(){
return false;
})
data()
method again to find the number:imageLinks.bind('click', function(){
var index = $(this).data('index'),
return false;
})
nextImage
since it will be the next image to show.imageLinks.bind('click', function(){
var index = $(this).data('index'),
var nextImage = stage.find('img:eq(' + index + ')'),
})
jQuery allows us to find an element by its index number using the :eq
selector.
For example, the $('img:eq(1)')
selector would select the second image in a
list of images. (Remember, JavaScript counting starts at 0 instead of 1.) In this case,
I know which number image I want because it's the number stored in the link that
was just clicked.
active
to it.imageLinks.bind('click', function(){
var index = $(this).data('index'),
var nextImage = stage.find('img:eq(' + index + ')'),
nextImage.fadeIn().addClass('active'),
return false;
})
active
when the image is shown, we can easily find the currently displayed image by looking for the one with the class of active:
imageLinks.bind('click', function(){
var index = $(this).data('index'),
var nextImage = stage.find('img:eq(' + index + ')'),
stage.find('img.active').fadeOut().removeClass('.active'),
nextImage.fadeIn().addClass('active'),
return false;
})
Don't forget that we'll have to be sure to remove that active
class so that only one
image will be marked active at a time.
If you refresh the page in the browser now, you'll see that clicking one of the thumbnail links in the carousel loads up the corresponding image in the slideshow. One image fades out while the next image fades in, in a nice smooth manner. Next, we'll get those next and previous buttons working so that we can use them to easily flip from one image to the next.
What just happened?
Phew! I hope you're still with me because this is a pretty awesome way to present a slideshow of images to your site visitors. I hope that you're starting to see that sometimes a plugin can be simply a beginning — you can get creative and invent your own functionality to layer on top of the default plugin behavior.
Next and previous buttons
We're definitely making some nice progress. Clicking the thumbnails loads up the full-size version of the image in the slideshow, and we can use the carousel controls to scroll through the thumbnails and see them all. Now, let's get the next and previous image buttons working.
18.225.255.134