Using other plug-ins with the JQuery Carousel Plug-in and event issues
This entry examines how the JQuery Carousel object interacts with other Plug-ins, and how to combine those plug-ins with the Carousel functionality. I also cover an issue where the jCarousel plug-in does not apply functionality to non visible elements on load.
I have been using two different JQuery Carousel plugins for a while now. The slightly older jCarousel ( http://sorgalla.com/jcarousel/) and the jCarousellite plug-in ( http://www.gmarwaha.com/jquery/jcarousellite/ ). Both work well, and both allow you to scroll through content in 'Rich' manner.

The problem
What I also want to combine with these is a lightbox ( FancyBox ). A user scrolls through the Carousel and clicks on one of the items, the item click invokes the lightbox script and the lightbox 'pops up'. This works for the items that are actually on display when the page loads. For all the other items it does not work. Clicking them simply takes you to their URL, and does not invoke the lighbox.Debugging
It appears that the Carousel code that hides the off screen elements also causes them to be skipped in the DOM when the lighbox is applied. When you view their source they appear to have the right class, but they simply don't work.The solution
Moving the position of the call to the lightbox will fix this. Usually you specify that a JQuery function loads when the document is ready, like this:$(document).ready(function(){
//my function code goes here
});
</script>
Instead we have to move our lightbox code to a position where it will be triggered on each Carousel movement. In that way the JQuery is applied to each newly visible element.
The code looks something like this:
// build an Array of data for the carousel
var mycarousel_itemList = [
{url: "http://static.flickr.com/66/199481236_dc98b5abb3_s.jpg", title: "Flower1"},
{url: "http://static.flickr.com/60/199480111_87d4cb3e38_s.jpg", title: "Flower2"},
{url: "http://static.flickr.com/70/229228324_08223b70fa_s.jpg", title: "Flower3"}
];
// run as each item loads
function mycarousel_itemLoadCallback(carousel, state)
{
for (var i = carousel.first; i <= carousel.last; i++) {
if (carousel.has(i)) {
continue;
}
if (i > mycarousel_itemList.length) {
break;
}
// Create an object from HTML
var item = jQuery(mycarousel_getItemHTML(mycarousel_itemList[i-1])).get(0);
// this runs whenever an item comes into focus
alert(i);
// this adds all the newly formed elements to the div
carousel.add(i, item);
// apply our lightbox effect to each of the new elements
$("a.iframe").fancybox({
'speedIn' : 600,
'speedOut' : 200,
'overlayShow' : true});
}
};
/**
* Item html creation helper.
*/
function mycarousel_getItemHTML(item)
{
var url_m = item.url.replace(/_s.jpg/g, '_m.jpg');
return '<a href="' + url_m + '" title="' + item.title + '" class="iframe"><img src="' + item.url + '" width="75" height="75" border="0" alt="' + item.title + '" /></a>';
};
// apply the carousel script to the correct div
jQuery(document).ready(function() {
jQuery('#mycarousel').jcarousel({
size: mycarousel_itemList.length,
itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback}
});
});
</script>
By positioning the lightbox code inside the call back function it is applied to each element as they gain visibility.
An demo of this is here.









