Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 21st, 2010 | Syntax: JavaScript | Size: 1.40 KB | Hits: 77 | Expires: Never
Copy text to clipboard
  1. $(document).ready(function(){
  2.   var currentPosition = 0;
  3.   var slideWidth = 560;
  4.   var slides = $('.slide');
  5.   var numberOfSlides = slides.length;
  6.   var isMoving = 0;
  7.  
  8.   // Remove scrollbar in JS
  9.   $('#slidesContainer').css('overflow', 'hidden');
  10.  
  11.  
  12.   // Wrap all .slides with #slideInner div
  13.   slides
  14.     .wrapAll('<div id="slideInner"></div>')
  15.     // Float left to display horizontally, readjust .slides width
  16.         .css({
  17.       'float' : 'left',
  18.       'width' : slideWidth
  19.     });
  20.  
  21.   // Set #slideInner width equal to total width of all slides
  22.   $('#slideInner').css('width', slideWidth * numberOfSlides);
  23.  
  24.   // Insert controls in the DOM
  25.   $('#slideshow')
  26.     .append('<span class="control" id="rightControl">Clicking moves right</span>');
  27.    
  28.   // Create event listeners for .controls clicks
  29.   $('.control')
  30.     .bind('click', function(){
  31.         if(!isMoving){
  32.         move();
  33.         }
  34.     })
  35.  
  36.   function move(){
  37.       isMoving=true;
  38.             currentPosition = currentPosition+1;
  39.             if(currentPosition == numberOfSlides){
  40.               currentPosition = currentPosition-numberOfSlides;
  41.             }
  42.     // Move slideInner using margin-left
  43.     $('#slideInner').animate({
  44.       'marginLeft' : slideWidth*(-currentPosition)
  45.     });
  46.     isMoving=false;
  47.   };
  48.  
  49.   function autoMove(){
  50.     if(!isMoving){
  51.         move();
  52.         setTimeout(autoMove,2);
  53.     }
  54.   }
  55. });