by ed0t - Published: October 10th, 2009

jQuery logo: write less, do more

In these months i have worked on an ecommerce project with a lot of products’ list. Since the customer had asked a carousel solution to show her products, we have chosen for jCarousel, an amazing plugin for jQuery.

jCarousel is great and highly configurable but it has a small lack: it does not remember the position of the carousel when a user came back to product list page after a quick view of a product detail.

I came out with a simple but practical solution that solves this problem using the support of cookies.

To do this we need few lines of Javascript.

First of all we have to manage our cookies. We can use pure javascript or jQuery thanks to jQuery Cookie. I’ll use the latter in the example above.

var inNum;
var outNum;
var COOKIE_NAME = "my_carousel_position";

// MANAGE COOKIES
function setCookie(cName, cValue, cDaysNum)
{
  var date = new Date();
  date.setTime(date.getTime() + (cDaysNum * 24 * 60 * 60 * 1000));
  $.cookie(cName, cValue, {  expires: date });
  return false;
}

function getCookie(cName)
{
  return $.cookie(cName);
}

function delCookie(cName)
{
  date = "Thu, 01-Jan-70 00:00:01 GMT";
  $.cookie(cName, null, {  expires: date });
  return false;
}
// END MANAGE COOKIES

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
  outNum = i;
  setPosition();
};

function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
  inNum = i;
};

// calculate first visible item
function getFirstVisibleItemId(inNum, outNum)
{
  minVal = Math.min(inNum, outNum);
  if(inNum == minVal){
    minVal--;
  }
  return minVal;
}

//set new carousel position in cookie
function setPosition()
{
  position = getFirstVisibleItemId(inNum, outNum)
  if(position >= 0 ){
    value = position+1;
    setCookie(COOKIE_NAME, value, 1);
  }
}

//retrieves carousel position from cookie
function getPosition()
{
  startVal = 1;
  var cookieVal = getCookie(COOKIE_NAME);
  if(cookieVal != ""){
    startVal = parseInt(cookieVal);
  }else{
    delCookie(COOKIE_NAME);
  }
  return startVal;
}

jQuery(document).ready(function() {
  startPosition = getPosition();
  jQuery('#mycarousel').jcarousel({
    scroll: 1,
    animation: 500,
    start: startPosition,
    itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback},
    itemVisibleInCallback: {onAfterAnimation: mycarousel_itemVisibleInCallback}
  });

});

First of all i declare three vars. The last of the three ( COOKIE_NAME ) is the name to assign to your cookie. Next there are three functions to manage cookies using jQuery Cookie plugin: getCookie, setCookie and delCookie.

Next there are mycarousel_itemVisibleInCallback and mycarousel_itemVisibleOutCallback. These functions simply retrieve the position of the carousel’s edges and store them in two global vars inNum and outNum previously declared.

mycarousel_itemVisibleOutCallback function also calls setPosition function that stores the correct carousel start value inside the specified cookie.

At the end there is getPosition function, that retrieves the starting position value from the cookie. This value is assigned to start parameter inside jcarousel initialization.

You can see an example here (i have edited an example of jCarousel website) or download it.

Comments: 11 Comments - Category: howto, programming

Rss Comments

11 Comments

  1. Hello from Russia!
    Can I quote a post in your blog with the link to you?

    1. Polprav
  2. Of course you can!

    2. ed0t
  3. Hi! This solution is great, i’ve been looking for it in whole internet! but i’ve got problem, it’s 3 days since try to solve it.
    I’m not proffesional programmer, so try to explain it in the easy way:)

    i’ve got carousel, but in this carousel in each column is 5 words, it looks like this:

    and, when i insert Your code, when skip between links(words in the list) whole carousel bounce in various direction.. Please help me with this:))

    i’ve tried to change numbers in this part of code:

    //set new carousel position in cookie
    function setPosition()
    {
    position = getFirstVisibleItemId(inNum, outNum)
    if(position >= 0 ){
    value = position+1;
    setCookie(COOKIE_NAME, value, 1);
    }
    }

    but i’m not sure if looking in good area..

    3. sprinte
  4. this is carousel (without brackets):

    ul id=”mycarousel” class=”jcarousel-skin-tango”
    li
    ul
    li id=”li_0″ /li
    li id=”li_1″ /li
    li id=”li_2″ /li
    li id=”li_3″ /li
    li id=”li_4″ /li
    /ul
    /li

    /ul

    4. sprinte
  5. Could you also provide the code for running the circular carousel with your cookie script? That would be great!

    I played around a bit already but the first item is always empty if the page loads for the first time…

    Greetings.

    5. Thomas
  6. Hey ed0t,

    That is exactly what I am looking for :) , thanks! Do have any idea if there is a way to make it work with jCarousel Lite (http://www.gmarwaha.com/jquery/jcarousellite/) It should support callbacks but I can’t seem to make ik work with your edit.

    Cheers juri

    6. Juri
  7. Thanks for posting this code. I had to make one change to get it to work for me, I don’t know why exactly but in the function getPosition() I changed the following

    if(cookieVal != “”){

    to if(cookieVal != null){

    I presume it’s because when you first load the carousel the cookie isn’t there…so returns null

    7. Nick
  8. How can i make this to work with 3 carrousels on 1 page?
    So there are three individual cookies?

    8. yourownmood
  9. I’m so sorry everybody but in this period I’ve been extrmely busy and at the same time WP decided not to notify me for your comments.
    I’m really sorry for this.

    @yourownmood:
    I imagine that a possible solution is to create three different cookies and to change the getPosition and setPosition passing a parameter (the cookie name).

    9. ed0t
  10. Hi,

    When I use your code my jquery stops working. My carousel does not function anymore.
    When I remove the extra lines start stop and the 2 callbacks, it works again.
    What am I doing wrong ?

    Regards,
    Ely

    10. Ely
  11. Sincerely i have no idea.
    Which version of jQuery / jCarousel are you using?
    Have you added the jQuery Cookie plugin?
    Where and which lines have you removed?

    Just another tip. If you use Firefox + Firebug and you open the Console tab of Firebug, when you reload the page probably it will give you an error.

    11. ed0t

Leave a Comment