jCarousel and using Images as External Controls
jCarousel is a fantastic addition to jQuery; it makes it very easy to set up a carousel of images, or indeed text, on your website. It is also a simple matter to add external controls to the system, that allow you to jump between the images and not just scroll: jCarousel external controls example
An issue that came up while putting in an image carousel on a website was how to use images as the external controls. The issue at hand basically arises from this one line:
carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
This line tells the carousel to use the integer value of the text found at each line that matches the id of our controller as the identifier for where in the carousel that control will move us to. What that means is that the text value of each line has to be a number, usually starting at 1 and increasing, by 1 at a time up to the number of images found in the carousel.
With some relatively simple changes to this line, we can use images with a specific 'alt' attribute and our external controls will still work:
carousel.scroll(jQuery.jcarousel.intval(jQuery(this).find('img').attr("alt")));
Your images must be set up to have the numerical value that image will move the carousel to upon clicked, i.e. in most situations, the first image will have 'alt="1"', the second will be 'alt="2"', etc...
I know this explanation is weak at the moment, I will explain this better and in more detail at a later stage, but right now I have to get my Ph.D. finished. If it is unclear, leave a comment outlining what you don't understand and I'll try to explain.


Comments
What if there's more stuff
What if there's more stuff beyond n inside alt=""
For example, the wordpress function I have looks like class="hentry p1 post publish author-td-exec category-blog category-featured untagged y2010 m07 d22 h06">
can I get jcarousel to look for "p1"???
external controls via css classes instead of alternate text
The alt tag is the alternate text the describes the image, the class tag adds classes to object, so I'm not quite sure which one you mean.
Assuming you mean that the external control images have classes associated with them, and the class 'p1' is for the first image and 'p2' is for the second etc... I'll try to explain.
First off I'll explain this line a little clearer:
If I had clicked on the second control, i.e. the number 2, the above line evalates to: carousel.scroll(2); i.e. scroll the carousel to the second image. What we have to do is emulate that behaviour, so when a click is detected on the second image in the external controls, we need the function to find the integer value 2. Assuming, as I said above, that you have a class that incrementally increases based on the position in the carousel it represents, i.e. first image, the class = p1, second image the class = p2 and so on. We need to check the clicked image for that class, and return an integer value that matches that.
Pseudo-code:
Second image clicked Check class If class p2 return 2or a more general version of pseudo-code:
image clicked switch class p1: return 1 p2: return 2 p3: return 3 endThe javascript would look something like the following (I haven't tested this at all):
carousel.scroll(function() { if (jQuery(this).hasclass("p1")) { return 1; } if (jQuery(this).hasclass("p2")) { return 2; } if (jQuery(this).hasclass("p3")) { return 3; } });It's not a great solution, since you can't have an arbitrary number of images in your carousel, and I'm sure there is a cleaner solution you could use, but it's early on a Saturday morning.
GOD BLESS YOU FOR THIS !
GOD BLESS YOU FOR THIS ! THANK YOU SOOOOO MUCH ! I needed this for my site !
Post new comment