Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 13th, 2013  |  syntax: None  |  size: 2.25 KB  |  views: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. carousel
  2.        
  3. function centerCarouselOn(index, callback) {
  4.   var items = $('li', carousel);
  5.   var middleIdx = Math.floor(items.length / 2);
  6.   var direction = null;
  7.   var iterCount = 0;
  8.  
  9.   if(index === middleIdx) return;
  10.  
  11.   // if iterCount is positive, we are going right; else, we are going left
  12.   iterCount = middleIdx - index;
  13.  
  14.   // this funciton gets called recursively until all moves are complete
  15.   function moveCarousel() {
  16.     if (iterCount===0) return;
  17.  
  18.     if (iterCount > 0) {
  19.       // take the last element, prepend it to the carousel
  20.       $('li', carousel).last().prependTo(carousel);
  21.       iterCount--;
  22.     } else if (iterCount < 0) {
  23.       // take the first element, append it to the carousel
  24.       $('li', carousel).first().appendTo(carousel);
  25.       iterCount++;
  26.     }
  27.  
  28.     // execute callback to apply css changes at each step
  29.     callback();
  30.  
  31.     // set a delay, then repeat.
  32.     window.setTimeout(moveCarousel, 1000);
  33.   }
  34.  
  35.   // start moving the carousel
  36.   moveCarousel(iterCount);
  37. }
  38.        
  39. function centerCarouselOn(index, callback) {
  40.   var items = $('li', carousel);
  41.   var numItems = carousel.children().length;
  42.   var middleIdx = Math.floor(items.length / 2);
  43.   var direction = null;
  44.   var iterCount = 0;
  45.  
  46.   if(index === middleIdx) return;
  47.  
  48.   if(index > middleIdx) {
  49.     direction = 'left';
  50.     iterCount = (index - middleIdx);
  51.   }
  52.   else {
  53.     direction = 'right';
  54.     iterCount = (middleIdx - index);
  55.   }
  56.  
  57.   $('li', carousel).each(function(k, v) {
  58.     var li = $(v);
  59.  
  60.     // Here I need to iterate n places to the left or right
  61.     // e.g:
  62.     // direction = left, iterCount = 3
  63.     // Then each li by index would need this sequence:
  64.     // 0: 6, 5, 4
  65.     // 1: 0, 6, 5
  66.     // 2: 1, 0, 6
  67.     // 3: 2, 1, 0
  68.     // 4: 3, 2, 1
  69.     // 5: 4, 3, 1
  70.     // 6: 5, 4, 3 (this one moves to center - index 3)
  71.  
  72.     if(direction === 'right') {
  73.       for(var i = k; i < (k + iterCount); i++) {
  74.         var thisIter = i;
  75.         var nextIter = (++thisIter >= numItems) ? (thisIter - numItems) : thisIter;
  76.  
  77.         console.log(k + ': ' + nextIter);
  78.  
  79.       }
  80.     }
  81.     else {
  82.       for(var i = k; i > (k - iterCount); i--) {
  83.         var thisIter = i;
  84.         var nextIter = (--thisIter < 0) ? (numItems + thisIter) : thisIter;
  85.  
  86.         console.log(k + ': ' + nextIter);
  87.       }
  88.     }
  89.   });
  90. }
clone this paste RAW Paste Data