2013-08-01 22 views
0

我做了jQuery的滑塊使用本教程jQuery的自定義滑塊無限循環

http://css-plus.com/2010/09/create-your-own-jquery-image-slider/

,其中的圖片會自動滑動,但只有一次。我怎樣才能讓他們在無限循環中循環?

上線例如:

www.vytvarkajablonec.jecool.net/ati

我真的很感謝您的幫助!

+0

使用的setInterval(),並且調用函數反覆 轉寄此:http://www.w3schools.com/jsref/met_win_setinterval。 asp –

+1

授予此答案http://stackoverflow.com/questions/2060140/jquery-rotating-banner-question/2060490#2060490 – Ruben

回答

0

只要將gallary設置回初始位置(如果它位於gallary的末尾)即可。

var oGallary = $('#gallery'); 
var gallarWidth = oGallary.width(); 

if(oGalary.position().left > stopPosition && oGallary.is(":animated") == false) 
{ 
    oGallary.animate({left : "-=" + imageWidth + "px"}); 
} 
else if (oGalary.position().left <= stopPosition && oGallary.is(":animated") == false) 
{ 
    oGallary.animate({left : "+=" + gallaryWidht + "px"}) // Get full length of the entire gallary 
} 
0

替換從教程中的JS代碼此:

$(document).ready(function() { 

    // Gallery 
    if (jQuery("#gallery").length) { 

     // Declare variables 
     var totalImages = jQuery("#gallery > li").length, 
      imageWidth = jQuery("#gallery > li:first").outerWidth(true), 
      totalWidth = imageWidth * totalImages, 
      visibleImages = Math.round(jQuery("#gallery-wrap").width()/imageWidth), 
      visibleWidth = visibleImages * imageWidth, 
      stopPosition = (visibleWidth - totalWidth); 

     jQuery("#gallery").width(totalWidth); 

     jQuery("#gallery-prev").click(function() { 
      if (jQuery("#gallery").position().left < 0 && !jQuery("#gallery").is(":animated")) { 
       jQuery("#gallery").animate({ 
        left: "+=" + imageWidth + "px" 
       }); 
      } 

      if (jQuery("#gallery").position().left === 0) { 
       jQuery("#gallery > li:last").prependTo($("#gallery")); 
      } 

      return false; 
     }); 

     jQuery("#gallery-next").click(function() { 
      if (jQuery("#gallery").position().left > stopPosition && !jQuery("#gallery").is(":animated")) { 
       jQuery("#gallery").animate({ 
        left: "-=" + imageWidth + "px" 
       }); 
      } 

      if (jQuery("#gallery").position().left === stopPosition) { 
       jQuery("#gallery > li:first").appendTo($("#gallery")); 
      } 

      return false; 
     }); 
    } 

}); 
+0

現在它在上一張幻燈片(這裏:http://www.vytvarkajablonec.jecool.net/ ATI /)。我的意思是當最後一張幻燈片到達時,下一張應該成爲第一張幻燈片。 (幻燈片1 - >幻燈片2 - >幻燈片3 - >幻燈片1 - >幻燈片2 etc ...) –

+0

爲什麼要再次追加項目?這隻會導致頁面使用更多的內存。正如在我的回答中所展示的那樣,你最好在最後重新啓動它,「讓它們在無限循環中循環」,是嗎? –

+0

@JiříOláh你改變了教程中的所有代碼?看看我的例子http://jsfiddle.net/sbml/NWntU/ – Sbml