2016-07-22 10 views
3

私は非常に基本的なスライドショーを作成するためにjQueryを使用しようとしています。jQuery Loop:スライドショーの最後の要素に達したら最初の要素に戻りますか?

私が現在持っている問題は、スライドショーが要素をループしないということです。

基本的に、内部にアイテムの要素が残っていなくても、空の領域に入ります。

これは、ボタンを数回クリックして、私が何を意味するかを見るべき作業FIDDLE

です。

空の領域に移動するのではなく、要素をループする方法はありますか?基本的にend/last要素を見つけて、最初の要素にループバックしますか?

これは私のコードです:

var click = 1; 

$("#SLIDE").click(function() { 
    var widths = $('.some').width() * click; 

    $(".some").first() 
      .animate({ "margin-left": "-"+widths }, "slow") 
      .next() 
      .animate({ "margin-left": 0 }, "slow") 
      .end(); 

    click+=1; 
}); 

任意の助けいただければ幸いです。

答えて

3

チェックJsFiddle Demo

ラップする剰余関数を使用します。

click = click % 4;

またはあなたはそれが動的に要素の数を取得したい場合は、代わりにこれを使用することができます:

JsFiddle Demo

click = click % $('.some h1').length;

HTMLを

<div align="center" id="feedTxt"> 

    <div class="some"> 
     <h1>title 1</h1> 
     <br> 
     <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
      It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently 
      with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p> 
    </div> 



    <div class="some"> 
     <h1>title 2</h1> 
     <br> 
     <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
      It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently 
      with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p> 
    </div> 


    <div class="some"> 
     <h1>title 3</h1> 
     <br> 
     <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
      It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently 
      with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p> 
    </div> 


    <div class="some"> 
     <h1>title 4</h1> 
     <br> 
     <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
      It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently 
      with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p> 
    </div> 

</div> 

<button id="SLIDE">Peress to slide</button> 

JS

var click = 1; 

$("#SLIDE").click(function() { 
    var widths = $('.some').width() * click; 

    $(".some").first() 
      .animate({ "margin-left": "-"+widths }, "slow") 
      .next() 
      .animate({ "margin-left": 0 }, "slow") 
      .end(); 

    click+=1; 
    click = click % $('.some h1').length; 

}); 

CSS

#feedTxt { 
    display: flex; 
    overflow-x: scroll; 
    height:450px; 
    width:100%; 
} 

.some { 
    flex: 0 0 100%; 
    height: 450px; 
} 
+0

歓声...これは私が探していた解決策です... – Jackson

0

ちょうどあなたが持っているどのように多くのスライド数えると、最後の1に達したときにカウンタをリセット:

var click = 1, 
    slides = $(".some").length; // Count slides 

$("#SLIDE").click(function() { 
    var widths = $('.some').width() * click; 

    $(".some").first() 
      .animate({ "margin-left": "-"+widths }, "slow") 
      .next() 
      .animate({ "margin-left": 0 }, "slow") 
      .end(); 

    click += 1; 
    if (click == slides) click = 0; // Reset the counter, if necessary 
}); 

あなたはそれをin this updated fiddle確認することができます。

0

あなたは基本的に自分でそれに答えは、ちょうど0に戻しclickを入れて、あなたが最初のスライドに戻ってジャンプします。ここで

はフィドルです:I go back to 0.

基本的に、そしてちょうどスライドの合計額よりもその大きな場合(クリック仮定することができますが、それを表す)したいスライドの数を取る - 0

if(click > 3){ 
    click = 0; 
    } 
に余裕を返します
関連する問題