jCarousel Lite – A jQuery plugin

jcarousellite-img-02

jCarousel Lite is a jQuery plugin that carries you on a carousel ride filled with images and HTML content. Put simply, you can navigate images and/or HTML in a carousel-style widget. It is super light weight, at about 2 KB in size, yet very flexible and customizable to fit most of our needs.

Did I mention that it weighs just 2 KB?

As if that wasn’t enough, the best part is yet to come… You don’t need any special css file or class name to get this to work. Include the js file. Supply the HTML markup. Then, construct the carousel with just a simple function call.

Visit the project page for more information. There you can find a lot of demos and exhaustive documentation. This blog entry is just a teaser for further exploration.

Installing and getting it to work is as trivial as following the 3 steps given below…

Step 1:

Include a reference to the jQuery library and the jCarouselLite plugin. You can download jQuery here and jCarouselLite here. If you like interesting effects, include the Easing plugin as well (Optional). You may also want to use a compatibility easing plugin if you want to use old easing names. If you would like to navigate the carousel using mouse wheel, then include the mouse-wheel plugin as well (Optional).

<script type="text/javascript" src="path/to/jquery.js"></script>
<script type="text/javascript" src="path/to/jquery.jcarousellite.js"></script>

<!-- Optional -->
<script type="text/javascript" src="path/to/easing.js"></script>
<script type="text/javascript" src="path/to/mousewheel.js"></script>

Step 2:

In your HTML file, provide the markup required by the carousel (a div enclosing an ul). You also need the navigation buttons, but these buttons need not be part of the carousel markup itself. An example follows…

<button class="prev">&laquo;</button>
<button class="next">&raquo;</button>

<div class="any-class">
    <ul>
        <li><img src="image/1.jpg" style="width:150px; height:118px;"></li>
        <li><img src="image/2.jpg" style="width:150px; height:118px;"></li>
        <li><img src="image/3.jpg" style="width:150px; height:118px;"></li>
        <li><img src="image/4.jpg" style="width:150px; height:118px;"></li>
        <li><img src="image/5.jpg" style="width:150px; height:118px;"></li>
    </ul>
</div>

Step 3:

Fire a call to the plugin and supply your navigation buttons. You just managed to architect your own carousel.

$(function() {
    $(".any-class").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev"
    });
});

If you have any comments/suggestions/requests, feel free to drop in a comment or reach @ganeshmax in twitter.

 

Update

Based on popular request, jCarouselLite has been updated to support jquery 1.2.x versions. Goto the download page and download version 1.0.1 to enjoy jquery 1.2.x support. Since Firefox 3 has some issues with $(document).ready() function, try using $(window).load() instead if you face any problems. Hopefully a future version of Firefox or jQuery will fix the problem.

Ganeshji Marwaha

I spend my days as the Director of Technology for Mobility practice and help my clients design enterprise and consumer mobile strategies. Mobile Payments, Digital Wallet and Tokenization technologies are my areas of specialization

  • Kristofer

    What if I would like to place a list inside the carousel?
    Imagine the following:

    First
    Second
    Third

    Some menu nr 1
    Some menu nr 2

    Fourth

    Trust me, that messes things up. I tried to add a not() clause to the jquery selector but with no luck. Any suggestions?

  • Kristofer

    Crap, he removed all my markup.. Let´s try again..
    div classname=jcarousellite
    ul
    li First endli
    li Second endli
    li Third endli
    ul
    // Don´t want this list to be “parsed”
    li Some menu nr 1 endli
    li Some menu nr 2 endli
    endul
    li Fourth endli
    endul
    endiv

  • JD

    This is awesome! Thanks so much for making this available.

  • @Kristofer

    Did you try Blackout’s hack? (post #294)

  • Wendy Neill

    Newbie to javascript, jQuery and jCarouselLite. Forgive my ignorance. I looked at lots of tutorials, but can’t find this simple answer. I got the basic configuration to work, but can’t seem to figure out how to implement the additional options, like moreItems. So in the head, I have:

    $(document).ready(function() {

    $(“.anyClass”).jCarouselLite({
    btnNext: “.next”,
    btnPrev: “.prev”
    });

    $(“.moreItems .jCarouselLite”).jCarouselLite({
    btnNext: “.moreItems .next”,
    btnPrev: “.moreItems .prev”,
    visible: 1
    });

    });

    Then in the body, I have

  • Wendy Neill

    (continued from previous post, don’t know what happened there….)
    In the body, I have

  • Wendy Neill

    Never mind…I found your comments and examples in the .js file which were very helpful and figured it out. Sorry for my ignorance. It is easy to get in a hurry and not be thorough.

  • Ben

    I have noticed a few people are having this trouble with the vertical static list appearing when loading into a table cell. Although I am getting it with firefox, so im sure IE wont work either. Upon pressing the next and prev buttons the list just hides and shows itself.

    I know that plug in is being called properly bc without the call for the script, the toggling of the list with the buttons doesnt work.

    I have gone over my code and several times and checked install notes and other demo scripts, but still have same problem.

    Here is my link to the page at issue

    http://royalvillicus.com/photo_site/photo.html

    I appreciate any help

  • I’ve added a quick mod for enabling/disabling buttons, when not circular, for the first slide (disable previous button), and also if there’s only one “slide” (disable next button):

    After this (~lines 42-26):
    if(o.circular) {
    ul.prepend(tLi.slice(tl-v-1+1).clone())
    .append(tLi.slice(0,v).clone());
    o.start += v;
    }

    Add:

    else
    {
    if(o.start == 0)
    {
    $(o.btnPrev).addClass(“disabled”);
    }

    if(tl == 1)
    {
    $(o.btnNext).addClass(“disabled”);
    }
    }

  • else
    {
    if(o.start == 0)
    {
    $(o.btnPrev).addClass(“disabled”);
    }

    if(tl == 1)
    {
    $(o.btnNext).addClass(“disabled”);
    }
    }

  • Mark C

    I am using JCLite to display 1 image (visible:1) in a sidepanel widget with a next prev button which allows them to look at otehr images. All is fine accept the initial page load displays the li items for about 1 second before jquery kicks in and manipulates the css. I think this is a common problem but I haven’t found a wokring solution, most people suggest using a semaphore approcah, hide() before the ready event, show() after, or using toggleClass on the container. But none seem to work.

    Another problem is trying to use the reflections.js plugin with an image inside jcarousellite, it doesn’t work correctly, it misses some refelctions off, anyone got this working.

  • Mark C

    As i’m using this more and more certain other options or functions I need. As i am using it for displaying 1 image in a sidebar widget, when the user goes to another page (the sidebar will always be there) how can I store the last carouselimage index and restart it from that index when a new page loads.

  • charles

    Thanks for this great work!!

    I want to diable the next button when images are ended (vice versa…)

    And also i want to give play & pause button

  • SuneR

    I am wondering if there is a new updated version in the works? I know that there is sample code posted here in the comments that adds a pause feature, but I was wondering if this change (and other new features) will result in a new version?

  • If any one is trying to get the autoness to work with external controls, here’s how….

    Assuming you have external controls with ids:
    “#pi1”, “#pi2”, “#pi3”, “#pi4”, “#pi5”, “#pi6”

    And the images with ids of:
    “#im1”, “#im2”, “#im3”, “#im4”, “#im5”, “#im6”

    Before you start the jcl function, count the no. of li’s:
    var LiCount = $(“#projectimage”).find(“ul”).children(“li”).size();

    Add the before start function:
    beforeStart: function(a) {
    var imID = $(a).attr(“id”).replace(‘im’,”);
    var NimID = parseInt(imID);
    NimID++;
    if(NimID > LiCount) NimID = 1;
    $(‘#pi’+imID).removeClass(‘activelistlink’);
    $(‘#pi’+NimID).addClass(‘activelistlink’);
    }

    Change the jcl source code, for the o.btnGo if statement:
    if(o.btnGo)
    $.each(o.btnGo, function(i, val) {
    $(val).click(function() {
    if(o.auto) clearInterval(autoIntervalID);
    var autoIntervalID = setInterval(function() { go(curr+o.scroll); }, o.auto+o.speed);
    return go(o.circular ? o.visible+i : i);
    });
    });

    Change the o.auto if statement in the jcl:
    if(o.auto)
    var autoIntervalID = setInterval(function() {
    go(curr+o.scroll);
    }, o.auto+o.speed);

    Done. This means when you click an external control the auto restarts. Also the class of the external control changes depending on the current image, so you can add some current styling.

  • you could also use the new o.auto if statement with a new function to pause. so make a button, add the button id to the variables sent to jcl. add this variable list to the jcl var list at the beginning. then in the source code make a click function for the variable and clear the interval as above

  • Kristofer

    @Mark Voss

    Thanks, that helped a lot.

    For you that might have the same problem, if you would like to have nested lists that should not be included in the the carousel, take a look at comment nr 294.

  • I found an issue with setting a scroll number. It won’t scroll the last items if there are less than the number set to scroll.

    For instance:

    6 items
    3 shown at once
    scroll: 2

    So, you first see: 1|2|3
    Then you see: 3|4|5

    And then the carousel is stuck. It won’t scroll to #6.

    Great plugin, btw! Thanks!

  • Aaron

    Is there any way to get the pause on over working with the Go buttons instead of the prev & next buttons? I’ve tried all the modified /patched scripts here, and the only one I could get working was the non-minified link Oren posted. But, I’m not using the prev & next buttons, instead I’m using 4 btnGo’s. Mousing over the div works fine, but mousing over the buttons doesn’t pause.

  • @charles (313)…see post #10.

    Regarding that, post #13 hints that there might be an update to allow the previous button, upon load, to also get the ‘disabled’ class. Was that ever implemented (doesn’t seem to be working).

    In the interim, it’s fairly easy to add this oneself. Just add the disabled class on document ready:

    $(“.yourButtonClass”).addClass(“disabled”);

  • hi ,
    I’m a jQuery Beginner and have really used your jCarouselLite extensively in websites I make.
    i’ve used jCarouselLite Plugin to show sponsors’ logos at bottom of the page, these are images between anchor tags.
    Also i have used another jscript for smooth scrolling of inner HTML anchors.

    http://amitech09.com/event.html

    Although Height of all images are same widths are different.
    After 1 complete cycle of images rotation Carousel acts weirdly and jumps randomly and with variable speeds.

  • Hi, I’m having an issue with the carousel. On first load, the carousel is actually not triggered for some reason. Its hidden until you reload the page. After that it will continue working. When you clear your cache and load the page, the issue happens again. Can anyone take a look and figure out why its not working on first load?

  • Sorry, forgot to post the site, http://edonnersberginteriors.com/

  • Well, I came up with a workaround for the “can’t scroll the last set of items if they are less that the number I told it to scroll”

    The fix is to pad the items with a couple of extra ones to make it divisible by your scroll number.

    var numberToScroll = 3

    $(function() {
    var n = $(“.jCarouselLite li”).length;
    var remainder = n%numberToScroll;
    var strExtraLIs = ”

    if (remainder > 0){
    //append li
    while((numberToScroll-remainder)>=1){
    strExtraLIs = strExtraLIs + ”;
    remainder = remainder+1;
    };
    };

    //append extra LIs
    $(‘.jCarouselLite ul li:last-child’).after(strExtraLIs);

    });

    $(function() {
    $(“.jCarouselLite”).jCarouselLite({
    btnPrev: “.older”,
    btnNext: “.newer”,
    scroll: numberToScroll,
    circular: false,
    speed: 500
    });
    });

  • Hi, I´m using carousel lite for http://www.farandulacriolla.com but when I activate Lightbox 2 plugin it stop working.
    Here is the plugin:
    http://m3nt0r.de/blog/projekte/lightbox-wordpress-plugin/
    Can you help me?
    Thanks!

  • Has anyone incorporated some AJAX calls into this to extend the list of items?

    I’ve done it, but ran into two main issues:

    1) When you instantiate carousel-lite, it appends in-line styles (or so I think) to each LI. If you append new items, those do not get those styles. To remedy that, I re-instantiate the carousel, but that feels like the wrong way to do it.

    2) When I do append more items, the scrolling acts a bit odd. If I’m scrolling 3 at a time:

    1-3 (click, scroll)
    4-6 (click, scroll)
    7-9 (click, load more items, scroll)
    …this is where it gets weird again…
    4-6 (click, scroll)
    10-12, then back to 7-9 (scroll)

    It does this odd ‘back then forward’ thing after I load more items. Any workarounds anyone know of?

  • Great plugin Ganesh!

    I read the comment from [ Aaron Shekey on February 22nd, 2008 at 7:52 am ] over a year ago and no response was given. I’m looking for the method to highlight the current external control using callback. I’m sorta surprised there isn’t an example of this anywhere.

    Help please!

  • Eug

    Really a great work Ganesh,
    I didn’t understand if it possible to set the slideshow width and put the image with different size that doesn’t fit exactly in the space.

    Any advice?

    E.

  • Jorge

    Great plugin!

    It works with different size images, just apply this styles:

    container ul>li{
    width:the max width of the images;
    }
    #media ul>li>img{
    margin:0 auto;
    display:block;
    }

    It works with horizontals carrousels.

  • Jorge

    Great plugin!

    It works with different size images, just apply this styles:

    container ul>li{
    width:the max width of the images;
    }
    container ul>li>img{
    margin:0 auto;
    display:block;
    }

    It works with horizontals carrousels.

  • nice effects. thanks alot

  • Hi, nice plugin, in file size and functionality 🙂

    I have one issue with it, though: when you set it to “auto,” it changes the frames regardless whether a user clicked just before or not. For example if you have set it to 10 seconds and the user happens to click the “next” button after 9 seconds, after 1 more second it displays the next frame again.

    Here is the patch that fixes it:

    @@ -271,10 +271,16 @@ $.fn.jCarouselLite = function(o) {
    return d>0 ? go(curr-o.scroll) : go(curr+o.scroll);
    });

    – if(o.auto)
    – setInterval(function() {
    + var autoTimeout = false;
    +
    + function auto() {
    + if (!o.auto) return;
    + if (autoTimeout) clearTimeout(autoTimeout);
    + autoTimeout = setTimeout(function() {
    go(curr+o.scroll);
    }, o.auto+o.speed);
    + };
    + auto();

    function vis() {
    return li.slice(curr).slice(0,v);

  • Oops, a part of the patch was missing. Here is the rest:

    @@ -322,6 +328,7 @@ $.fn.jCarouselLite = function(o) {
    ).addClass(“disabled”);
    }

    + auto();
    }
    return false;
    };

  • Lynn

    Has anyone thought about upgrading the documentation? It just does not work as in the current version without knowing more than what’s in the documentation.

    Example:
    $(“.default .jCarouselLite”).jCarouselLite({
    btnNext: “.default .next”,
    btnPrev: “.default .prev”
    });

    That’s all there is. That’s not the complete PHP and there is no HTML shown.

    It just leave me frustrated. I would like to use the Mixed Content option but that page like all pages is protected from letting me look at the underlining source.

    Perhaps there is a good tutorial somewhere?

  • Krzysztof Rajda

    Hi I really like your plugin. I needed to call function go() from my code, so I have modified your plugin a little bit. Do you think that you can add this to your code?

    function go(to) {
    // body of the function
    };

    // workaround to make go function accesible
    $.extend(jQuery(this).get(0), {
    showCarouselElement : function(index) {
    if (!running) {
    go(new Number(index));
    return true;
    }
    return false;
    }
    });

  • Hi.
    It’s a wonderful plugin.
    The only thing that’s missing is the ability to stop scrolling when the mouse is over it’s window.
    The patches and examples to make it work with on hover pause I found, dont work with jquery ver 1.2+.

    Sergio

  • niraj

    Hi Ganesh,
    Sorry I am asking a question a bit out of context. I am new to jquery and javascript. I have written code for a circular carousel in jquery for my pictures. I use jquery 1.3.2. I attach the hover and click events to the jquery items so that they can change color when hovered onto and also enlarge when clicked. One problem with jquery is that remove() kills all events attached with the items. Since its a circular carousel, I have to remove the items which go out of visible area and I add them at the back of the carousel list. I can see that the class on which i add the events is still correctly attached at the back, but the events dont fire. Is there a workaround to this problem.

    Great if you can reply on my e-mail

  • scott galambos

    I have a problem with “IE drift”. When scrolling in verticle mode there seems to be a couple pixel padding between my ‘s. but only in IE6/7. I put tables in my li’s so I don’t know if that matters. I’ve tried setting padding and margin to 0px everywhere and nothing helps. So by the time I scroll to like the 7th item its off 7 pixels or so. same problem here originally was posted by “John Pendergrast on August 15th, 2007 at 7:55 pm”.

    any idea?

  • Jdoggydog

    Boy, it must really suck to be a javascript developer who created a useful tool to save people who don’t know how to code hundreds of dollars of development and time, only to have them start demanding edits to your free work. I’d tell ’em all to learn how to code or go to Hell.

  • John D

    Hello, Im trying to use this plugin and I’m noticing that this isn’t working in the current version of Firefox that I’m using (Firefox/3.0.8) on my Mac. Do you have a workaround for this yet?

  • Hi,

    I have implemented the basic script without problems, but following the instructions for the auto scroll and it doesnt work. The carousel loses its structure (it ends up vertical instead of horizontal) and it doesnt auto scroll.

    Can you make the instructions alot clearer on whats required to make the auto scroll work please?

    Thanks.

  • Nuo

    I was wondering so far we had different favours of carousel. But I have not see non something like cnn.com has on top right video section.

    Showing only 3 top videos link at a time, and either click on next button or gray dot will take you to next page.

    Let me know if you know or have any such carousel. Thx 4 ur help.

  • Caleb

    Has anyone noticed that Ganesh hasn’t replied in almost half a year? I think we all need to be looking for a plugin that is better maintained and doesn’t have as many issues. That, or learn to program better 🙂

  • Jamie

    @Marko #85

    I’ve experience the same issue as you, whereby if you have for example 7 images in your carousel, with only 6 visible, and scroll set to 3 it will not scroll to the remaining one image.

    I’ve rectified this by changing some of the source code. Alter the following code (lines 299 to 302):

    } else {
    if(toitemLength-v) return;
    else curr = to;
    }

    So it now becomes:

    } else {
    if (to>itemLength-v) {
    to=(itemLength-v);
    }

    if (to<0) {
    to=0;
    }

    if(toitemLength-v) return;
    else curr = to;
    }

    This should make it scroll however many images are remaining if it is less than the scroll value, and in both directions.

    It would be great if Ganesh would consider implementing this. Assuming he is still maintaining this plugin!?

  • Nuo

    I was just wondering for your jCarousel Lite example of “External Controls” is it possible to get to page instead of item #.

    Example if you’r showing 3 items at a time with total 12 items. Ofcourse Next Button will get you to next three items and same with Pre. But when I click the gray dots(custom made css) or your [1][2][3] clicking on 2 should to go 2nd page instead of 2nd item.

    Any such modification code ??

    thanks,
    Nuo

  • Dan

    Is anyone else having display issues in firefox 3. Something in the javascript is causing my to be set to the incorrect size which in turn, cuts off the images. I can’t seem to figure this out.

  • I am using this plugin with an easing plugin, but the “expoout” and “expoin” easing types won’t work. Anyone know why?

  • potion

    Can anyone tell me how can I change the number of visible items depending on browser width?
    My default is:

    $(“.browseClass”).jCarouselLite({
    visible: 4,
    scroll: 1
    });

    but I need to change “visible: 4” to “visible: 3” or auto 😀 when browser resolution is bellow 1200 let’s say

  • mak

    hey,

    Great plug-in but I getting an error on line 336 (5).
    ‘0.offsetWidth’ is null or not a object.

    can you please tell me what to fix this. its only giving error in IE.

  • potion, why not use js to get the width then divide it by the width of your images. this can give you your number visible