2014-03-02 63 views
1

嗨我做了一個小的img滑塊使用jQuery prev和下一個按鈕。滑塊自動向前,下一步按鈕也可以,但是在上一個按鈕是靠不住的......下面是對的jsfiddle的鏈接,因爲它有點長:通過img滑塊向後走jQuery

http://jsfiddle.net/davidpm/5TsL6/

...和強制性的片段:

var sliderCentral = {        

    nextImage: function(d) { 
      var index = currIndex + d; 

      if (index <= 0) {  
      index = totalImg;  
      }; 
      if (index >= totalImg) {  // totalImg = 4 
      index = 0; 
      }; 
      currIndex = index; 
      $slider.fadeIn(500); 
    }, 

我已經評論了我認爲問題在哪裏。這應該是一個快速修復(對於知道他們在做什麼的人)...任何幫助?

由於提前,

-D

+0

有一些問題與推進,並延緩'currIndex'。請參閱:http://jsfiddle.net/moob/5TsL6/4/ – Moob

+0

這可以幫助:http://jsfiddle.net/drjj7/ – veysiertekin

回答

2

這裏是解決方案:Jsfiddle link

 nextImage: function (d) { 
      var index = currIndex + d; 

      if (index < 0) { 
       index = totalImg - 1; 
      } 
      else if (index >= totalImg) { // totalImg = 4 
       index = 0; 
      } 

      //console.log(index);    // see what happens 
      currIndex = index; 
      var Img = imgArr[currIndex]; // looky here??    
      $slider.attr("src", Img); 
      $slider.fadeIn(100); 
     }, 
     forwardsGo: function() { 

      $slider.fadeOut(100, function() { 
       sliderCentral.nextImage(1); 
      }); 
     }, 

     backwardsGo: function() { 

      $slider.fadeOut(100, function() { 
       sliderCentral.nextImage(-1); 
      }); 
     }, 
+0

謝謝Gyandeep!奇蹟般有效! – davidpm4

0

有一些問題,推進和延緩currIndex

 forwardsGo: function() {     

      $slider.fadeOut(500, function() {    
      var nextIndex = (currIndex+1 > totalImg)?0:currIndex+1; //new 
      var currentImg = imgArr[nextIndex];  //amended   
       $slider.attr("src", currentImg);    
       sliderCentral.nextImage(1);     
      }); 
     }, 

     backwardsGo: function() { 

      $slider.fadeOut(500, function() { 
      var prevIndex = (currIndex-1 < 0)?totalImg:currIndex-1; //new 
      var prevImg = imgArr[prevIndex]; //amended    
       $slider.attr("src", prevImg); 
       sliderCentral.nextImage(-1);        
      }); 
     }, 
.... 

參見:http://jsfiddle.net/moob/5TsL6/4/