Guest User

Untitled

a guest
Jun 30th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 3.61 KB | None | 0 0
  1.  
  2. $(document).ready(function(){
  3.      
  4. var $carrousel = $('.carrousel'),
  5.  
  6.     $li = $('.carrousel li'),
  7.     indexLi = $li.length - 1,
  8.     i = 0, // on initialise un compteur
  9.     $currentLi = $li.eq(i);
  10.  
  11. $li.css('display', 'none');
  12. $currentLi.css('display', 'block');
  13.  
  14.         for(var j=0;j<$li.length;j++) {
  15.         $("#switcher").append('<div class="switch" id="'+j+'"></div>');
  16.  
  17.                 $('.switch').on("click", function(){
  18.                     $li.stop().fadeTo('slow', 0);
  19.                     $currentLi = $li.eq($(this).attr('id'));
  20.                     $currentLi.stop().fadeTo('slow', 1);
  21.                     $currentSwitcher = $(this);
  22.  
  23.                     for (var k=0;k<$switcher.length;k++) {
  24.                         if ($currentSwitcher!=$switcher.eq(k)) {
  25.                             $switcher.eq(k).removeClass("activeswitch");
  26.                             $currentSwitcher.addClass("activeswitch");
  27.                         }
  28.                     }
  29.                 });
  30.         }
  31.         var $switcher =$('#switcher .switch'),
  32.         $currentSwitcher = $switcher.eq(i);
  33.        
  34.        
  35.        
  36. $('.next').on("click", function(){
  37.  
  38.     i++;
  39.     if( i <= indexLi){
  40.         $li.stop().fadeTo('slow', 0);
  41.         $currentLi = $li.eq(i); //
  42.         $currentLi.stop().fadeTo('slow', 1);
  43.                                
  44.                 $currentSwitcher = $switcher.eq(i);
  45.                 $currentSwitcher.addClass("activeswitch");
  46.                 for (var k=0;k<$switcher.length;k++) {
  47.                     if ($currentSwitcher!=$switcher.eq(k)) {
  48.                         $switcher.eq(k).removeClass("activeswitch");       
  49.                         $currentSwitcher.addClass("activeswitch");
  50.                     }
  51.                 }
  52.  
  53.     } else {
  54.         i = 0;
  55.     $li.stop().fadeTo('slow', 0); // on cache les titres
  56.     $currentLi = $li.eq(i); // on définit le nouveau titre
  57.         $currentLi.stop().fadeTo('slow', 1); // puis on l'affiche
  58.                
  59.                 $currentSwitcher = $switcher.eq(i);
  60.                 $currentSwitcher.addClass("activeswitch");
  61.  
  62.                 for (var k=0;k<$switcher.length;k++) {
  63.                     if ($currentSwitcher!=$switcher.eq(k)) {
  64.                         $switcher.eq(k).removeClass("activeswitch");
  65.                         $currentSwitcher.addClass("activeswitch");
  66.                     }
  67.                 }
  68.     }
  69.  
  70. });
  71.  
  72. $('.prev').on("click", function(){
  73.  
  74.     i--;
  75.  
  76.     if( i >= 0 ){
  77.     $li.stop().fadeTo('slow', 0);
  78.     $currentLi = $li.eq(i);
  79.         $currentLi.stop().fadeTo('slow', 1);
  80.                
  81.                 $currentSwitcher = $switcher.eq(i);
  82.                 $currentSwitcher.addClass("activeswitch");
  83.                 for (var k=0;k<$switcher.length;k++) {
  84.                     if ($currentSwitcher!=$switcher.eq(k)) {
  85.                         $switcher.eq(k).removeClass("activeswitch");
  86.                         $currentSwitcher.addClass("activeswitch");
  87.                     }
  88.                 }
  89.     } else {
  90.         i = indexLi;
  91.     $li.stop().fadeTo('slow', 0);
  92.     $currentLi = $li.eq(i);
  93.         $currentLi.stop().fadeTo('slow', 1);
  94.                        
  95.                 $currentSwitcher = $switcher.eq(i);
  96.                 $currentSwitcher.addClass("activeswitch");
  97.                 for (var k=0;k<$switcher.length;k++) {
  98.                     if ($currentSwitcher!=$switcher.eq(k)) {           
  99.                         $switcher.eq(k).removeClass("activeswitch");
  100.                         $currentSwitcher.addClass("activeswitch");
  101.                     }
  102.                 }
  103.     }
  104.  
  105. });
  106.  
  107. function slideImg(){
  108.     setTimeout(function(){
  109.                          
  110.         if(i < indexLi){
  111.             i++;
  112.     } else {
  113.             i = 0;
  114.         }
  115.  
  116.     $li.stop().fadeTo('slow', 0);
  117.  
  118.     $currentLi = $li.eq(i);
  119.     $currentLi.stop().fadeTo('slow', 1);
  120.     if($currentLi==$li.eq(indexLi)) { $currentLi = $li.eq(0); }
  121.                        
  122.                     $currentSwitcher = $switcher.eq(i);
  123.                     $currentSwitcher.addClass("activeswitch");
  124.                     for (var k=0;k<$switcher.length;k++) {
  125.                         if ($currentSwitcher!=$switcher.eq(k)) {
  126.                             $switcher.eq(k).removeClass("activeswitch");
  127.                             $currentSwitcher.addClass("activeswitch");
  128.                         }
  129.                                    
  130.                     }
  131.     slideImg();
  132.  
  133.     }, 5000);
  134.  
  135. }
  136.  
  137. slideImg();
  138.  
  139.              
  140. $currentSwitcher = $switcher.eq(i);
  141. $currentSwitcher.addClass("activeswitch");
  142. });
Advertisement
Add Comment
Please, Sign In to add comment