2016-07-22 1 views
0

jqueryを使用してスライドアウトし、次の要素とループでスライドしようとしています。 。jQuery:スライドアウトして次の要素にスライドしてループしますか?

現在、私はOUT要素が、内の次の文句を言わないのスライドをスライドさせることができる

をこれが作業FIDDLE

であり、これは私のコードです:

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


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


}); 

誰かが上の助言してもらえこの?

ありがとうございます。

+0

この "' $」 1次回() .animate(してみてください(" 一部。){ "のmargin-left":-widths}、 "" 遅い) .nextを( '.some') .animate({ "マージン左":-widths}、 "遅い") .END(); \t}); ' – kukkuz

答えて

1

スライドにこのアプローチをお試しください。ここで

var click = 1; 
 
$("#SLIDE").click(function() { 
 
    if (click == $(".some").length) { 
 
var widths = 1; 
 
click = 1; 
 
    } else { 
 
var widths = $('.some').width() * click; 
 
    } 
 
    $(".some") 
 
.first() 
 
.animate({ 
 
    "margin-left": "-" + widths 
 
}, "slow") 
 
.next() 
 
.animate({ 
 
    "margin-left": 0 
 
}, "slow") 
 
.end(); 
 

 
    click += 1; 
 
});
#feedTxt { 
 
    display: flex; 
 
    overflow-x: scroll; 
 
    height:450px; 
 
    width:100%; 
 
} 
 

 
.some { 
 
    flex: 0 0 100%; 
 
    height: 450px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<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>

作業jsFiddle

は、それが役立ちます:)

+0

私は怖いこのあろうないループ。 – Jackson

+0

私はこの方法が大好きです。どのように私が知っていますか?要素の終わりに達すると、最初の要素に戻ることができますか? – Jackson

+0

@Jacksonちょうどあなたが望むように更新されました。見てみな – Thinker

1

希望この1つはあなたを助けを願っています。

$(function() { 
 
    // count your slide elements 
 
    var elementCount = $('.some').length; 
 
    // width of the first element 
 
    var width = $('.some').first().width(); 
 
    // full width of all elements 
 
    var fullWidth = elementCount*width; 
 
    // scroll left value 
 
    var scrollLeft = width; 
 

 
    // update values after resizing 
 
    $(window).on('resize', function() { 
 
    width = $('.some').first().width(); 
 
    scrollLeft = width; 
 
    fullWidth = elementCount*width; 
 
    $("#feedTxt").scrollLeft(0); 
 
    }); 
 

 
    
 
    $("#SLIDE").click(function() { 
 

 
    // if full width smaller or equal to your scroll left, than scroll to the beginning 
 
    if((fullWidth) <= scrollLeft) { 
 
     scrollLeft = 0; 
 
    } 
 

 
    // animate scroll left 
 
    $("#feedTxt").animate({scrollLeft: scrollLeft}, 800); 
 

 
    // if the full width not equal to scroll left, add width to scrollLeft 
 
    if((fullWidth) !== scrollLeft) { 
 
     scrollLeft += width; 
 
    } 
 
    }); \t 
 
});
#feedTxt { 
 
    display: flex; 
 
    overflow-x: scroll; 
 
    height:450px; 
 
    width:100%; 
 
} 
 

 
.some { 
 
    flex: 0 0 100%; 
 
    height: 450px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<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">Press to slide</button>

関連する問題