RTLing jCarousel (jQuery Plugin)

 jCarousel is a beautiful jQuery plugin. Personally it's one of my favorites! You can set up your caoursel up and running in just one line or two. Even theming it is easy. According to the project's page:

jCarousel is a jQuery plugin for controlling a list of items in horizontal or vertical order. The items, which can be static HTML content or loaded with (or without) AJAX, can be scrolled back and forth (with or without animation).

The only thing missing in this wonderful plugin is the RTL support. And in this tutorial, it won't miss it anymore.

1. Get the files

First of all, download the latest version of the jCarousel plugin: jcarousel.tar.gz or jcarousel.zip. Extract the downloaded folder. What we'll be working on in our RTLing process are the folders lib(where the JS is) and skins(where the CSS is)

2. Update the JavaScript plugin file

Open the lib folder to find the jquery library minified(latest version is 1.4.2 at the time of writing this tutorial), jqarousel library and the jqarousel library minified. We'll ignore the minified files and edit the jquery.jqarousel.js file(we'll minify it when we're done).

I won't bore you with the details of editing this file, but it's dead simple. It's all about spotting the explicitly declared "left" directional properties and make sure it can be dynamically set/configured.

We'll first start by adding an option to the defaults(line #53) called horizontalDirection and we'll set the default value to be 'ltr',

 
var defaults =
{ vertical: false,
 horizontalDirection:'ltr',
 //rest of default options
 

Then run a search for the string 'left' in the file. You'll get 3 results; Replace the first and third occurrences(lines #101 and #812) with the below:

(this.options.horizontalDirection=='rtl'?'right':'left')

Line #101 will be:

this.lt = !this.options.vertical ? (this.options.horizontalDirection=='rtl'?'right':'left'): 'top';

And line #812 will be:

 'float': (this.options.horizontalDirection=='rtl'?'right':'left'),

And in the second occurrence(line #682), replace the {'left':p} with:

(this.options.horizontalDirection=='rtl'?{'right': p}:{'left': p})

So it'll be:

 var o = !this.options.vertical ? (this.options.horizontalDirection=='rtl'?{'right': p}:{'left': p})  : {'top': p};

As it shows, we're simply checking for the option we added before setting the property explicitly(which was always a left). 

To have  CSS control on the new feature, we'll add a class to the container when its initialized named: jcarousel-direction-rtl or jcarousel-direction-ltr (depending on the horizontalDirection option). At line #163, add the below to JQ chain as below:

this.container.addClass(this.className('jcarousel-container'))
.addClass(!this.options.vertical?'jcarousel-direction-'+this.options.horizontalDirection:'')
.css({

And that's about it. Minify the file or pack it to use it on your live site.

3. RTL the skins

The plugins comes with two themes: tango and ie7. The modifications will be minor since they only target the previous and next buttons. The modifications below apply to both themes or any other theme with any theme_name.

The CSS modifications will be simply appending the below to the skin.css file:

.jcarousel-skin-theme_name .jcarousel-direction-rtl {direction:rtl;}
.jcarousel-skin-theme_name .jcarousel-direction-rtl .jcarousel-item-horizontal{ margin-right:0; margin-left:10px;}

/*horizontal buttons*/
.jcarousel-skin-theme_name .jcarousel-direction-rtl .jcarousel-next-horizontal{
background-image:url(prev-horizontal.png); right:auto; left:5px;
}
.jcarousel-skin-theme_name .jcarousel-direction-rtl .jcarousel-prev-horizontal{
background-image:url(next-horizontal.png); left:auto; right:5px;
}

As shown above, we're just overriding the CSS of the theme itself. Values may differ from theme to theme of course but the concept would be the same(Overriding the margin-right to be a margin-left is what matters)

As you've noticed too we're switching the background images of the next and previous buttons. This is to avoid having extra flipped images(we already have them )

...And you're done with the CSS

4. Use it!

Now to activate the RTL support, just use the new option we added:

jQuery(document).ready(function() {
  jQuery('#mycarousel').jcarousel({horizontalDirection:'rtl'});
});

5. Enjoy it :)

That's all :) The source code of the RTLed plugin and two themes are available to be downloaded and a demo is also available to see it in action(links below). I've tested this patch under Firefox, Chrome, Safari, IE7 and IE8. Should you face any bugs or trouble with it, please do add a comment with the issue below to be fixed ASAP.

Average: 2.9 (10 votes)

Comments

Hi,

i'm the author of jCarousel. Your post motivated me to finally implement RTL support for jCarousel (had it on my TODO list for a long time).

See: http://github.com/jsor/jcarousel/commit/c08b9cb61abd71394b644ccd175396c6...

The implementation is based on your code here. Additionally i implemented autdetection of RTL based on the dir attribute of the carousels <ul> and the <html> tag.

RTL support will be included in the release of jCarousel.

-Jan

Jan! I cannot express how happy I am right now..Thanks for including the RTL support in this wonderful plugin :)
It will be perfect for multilingual websites in the area here which mainly include Arabic and English/French interfaces.
Thanks again :)

jCarousel 0.2.5 which contains the RTL support was officially released today. You can grab it from http://github.com/jsor/jcarousel/downloads

-Jan

Hi, What exactly does RTL mean? Also, is there any way I can do something like http://www.chicagobooth.edu/ this slide show using jCarousel? What I am looking for is - a) slides that occupy the whole are and b) no previous and next buttons.

Thanks

RTL stands for "Right To Left", which is the direction of some languages such as Arabic and Persian About RTL This"]. This site. What you need is a slideshow not a carousel. Try these plugins at: http://www.webanddesigners.com/15-jquery-slideshow-and-plugins

This is great!! jCarousel totally rocks!

It doesn't work in Opera :(

Hi i am new to this and don't know anything about jquery. Are the above codes for html pages or do you save them to different files and upload them?

for Opera browser You have to add:

.jcarousel-skin-tango .jcarousel-list-horizontal {
right:0;
}

Thanks for the feedback =)
I'll update the demo and source. Although I strongly recommend you using the official latest release of jCarousel with RTL support

Actually, the reason for jCarousel not working in opera is that this.first is set to null by default, giving an error on function next(). The way to fix this is :

find:
next: function() {

replace with:
next: function() {
if (this.first == null) { this.first = 1; }

Thanks Bruno for the tip..I haven't tested it but is it a problem for the plugin in both LTR and RTL directions? or just triggered in RTL?
If it's a general plugin bug, it would be better if the plugin author is notified of this bug as well =)

I've got jCarousel 0.2.8 running rtl which produces this strange behavior: if any html block ends with any non-alphanumeric character, that character shows up at the beginning of the rendered line. For example,

See what I mean?

renders as

?See what I mean

whereas

See what I mean ?+a

renders as

See what I mean?+a

?Weird, huh

You can see the bug in action at www.alarmwillsound.com/template2col.html on the second slide.

-Gavin

Add new comment

Twitter Updates

Recent comments

Who's online

There are currently 0 users online.