Login
Home
Eoin Bailey . com
Tech, Research, Code, Work, and Fun
  • Home
  • Eoin's CV
  • About Eoin
  • Ph.D.
  • Blog
  • Galleries
  • Training
  • Polls
  • Tags
  • Weblinks
  • Site map
  • Contact

Tag Cloud

algorithm antarctica apple banking browser code copyright cycle data centre devel Dijkstra drupal drupalcamp economics escapades facebook firefox galway Google iphone ipod livigno theme training weights
more tags

A Random Image

IMG_3702

jCarousel and using Images as External Controls

Submitted by Eoin on Mon, 01/02/2010 - 09:31

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.

  • Eoin's blog
  • Printer-friendly version
  • Send to friend

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

What if there's more stuff

Submitted by Visitor (not verified) on Fri, 23/07/2010 - 16:16.

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"???

  • reply

external controls via css classes instead of alternate text

Submitted by Eoin on Sat, 24/07/2010 - 09:55.

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:

carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));

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 2

or a more general version of pseudo-code:

image clicked
    switch class
         p1: return 1
         p2: return 2
         p3: return 3
    end

The 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.

  • reply

GOD BLESS YOU FOR THIS !

Submitted by Visitor (not verified) on Thu, 21/07/2011 - 18:22.

GOD BLESS YOU FOR THIS ! THANK YOU SOOOOO MUCH ! I needed this for my site !

  • reply

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.

More information about formatting options

Current Poll

What is your current phone OS?:

Freelance Work

A sample of websites I have developed:

  • Studio Richards
  • Medilex
  • Design21C
  • The Spine Clinic
  • Abrivia - Careers and Outplacement
  • Emilie Conway

Twitter

Follow @eoinbailey

Some Links

  • James Bond Opening Scenes
  • Polls
  • Chess Module-Drupal
  • Ski Trip Jan 2009

Training

  • Spinning
  • Hodson Bay Hotel Training
  • Spinning Class - Not too Shabby!
  • Bike Time Trial
  • Spinning Class of Anti-Doom!

Recent Comments

  • Happy New Year 2012!
  • Thank you..
  • Simple way
  • Yep, CSS is an option - there
  • Maybe CSS is an easy solution?
  • Thanks for the direction.
  • Thank you for the insight.
  • Exactly.
  • Thank you for the insight.
  • I'm open to correction on

Powered by

Powered by Drupal, an open source content management system

Recent blog posts

  • Copyright Issues
  • Leap Card
  • A new College term - Dijkstra's Algorithm
  • Dublin GTUG - June 2011 - Over 100 attendees
  • City of a Thousand Welcomes
  • Groupon et al. - "Bet on the Future"
  • Dublin Web Summit - DWS6 - Roundup
  • Anti-Spam - Using a Catch-All to identify bad companies
  • Hide a Note Title - Drupal 7
  • The Election on Hit The Road
more
Copyright © 2012 Eoin Bailey . com.

I'm trying out: Web Analytics