1

Using jCarousel with galleria jQuery plugins

Posted August 4th, 2009. Filed under JavaScript

Galleria – a JavaScript image gallery, written in jQuery, creates a gallery (with thumbnails) from a list of images. It’s simple to use, you just attach the script, call it and point it to a <ul> on the page. Like this:

<link href="galleria.css" rel="stylesheet" type="text/css" media="screen"> 
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript" src="jquery.galleria.js"></script> 
<script type="text/javascript"> 
jQuery(function($) { $('ul.gallery').galleria(); }); 
</script>

Combining this with jCarousel, which is another jQuery plugin, and you can scroll through a list of thumbnails at the same time. The two compliment each other nicely.

jCarousel works in a similar way to Galleria. Attach it, call it and point it to a list, so when using with Galleria, point both plugins to the same list.

Combining the two took a bit of tweaking and fiddling, but was overall a simple enough task.

The main problems arose from the css that both plugins require (each plugin comes with a stylesheet included). I used the tango skin that came with jCarousel and copied the contents of the css file to the galleria stylesheet, so I had everything required for the gallery on one. I then stripped down the stylesheet and altered what I needed, step-by-step. Firebug was an invaluable tool in helping me find what styles were actually being used and what weren’t. Once I had got it working okay I inserted the gallery into the website and I was done.

You can see the final result at http://www.mikereedlondon.com/Html/AnIndianWedding.html

Here’s the full code inserted into the <head> tags.

<script type="text/javascript" src="../Assetts/jquery.js"></script>  // Attach the jQuery file - downloadable from the jQuery website
<script type="text/javascript" src="../Assetts/jquery.jcarousel.js"></script>  // Attach the jCarousel file
<script type="text/javascript" src="../Assetts/jquery.galleria.js"></script> // Attach the galleria file
 
// Call the plugins
<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery('ul#gallery').jcarousel({
        // This calls the jCarousel script, points it to ul#gallery and displays and scrolls 8 thumbnails at a time
scroll: 8,
visible: 8
 
    });
});
 
// Now lets call the galleria code
jQuery(function($) { $('ul#gallery').galleria({
			history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // image effects for the main image are added here
 
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
 
				// fetch the thumbnail container
				var _li = thumb.parents('li');
 
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
 
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
 
				// fetch the thumbnail container
				var _li = thumb.parents('li');
 
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
 
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
 
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		});
	});
 
</script>
If you have enjoyed this entry. Please feel free to bookmark it using your favorite social bookmarking site

One Response so far

  1. Oh dude this is GORGEOUS!
    I’ve been looking for something like this for more than one month now.
    I’ll credit you in my code.
    Thank you.

Leave a Comment