javascript - jQuery:滑出并滑入下一个元素并循环?

标签 javascript jquery

我正在尝试使用 jquery 滑出,然后滑入下一个元素并循环。

目前我可以将元素滑出,但下一个元素无法滑入。

这是一个工作FIDDLE

这是我的代码:

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


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


}); 

有人可以就此提出建议吗?

提前致谢

最佳答案

希望这篇文章能帮到你。

$(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;
    }
  });	
});
#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>

关于javascript - jQuery:滑出并滑入下一个元素并循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38524673/

相关文章:

javascript - 动态分配按钮 ID 并通过 $.proxy() 传递它

javascript - Siesta:组件查询断言不起作用

javascript - Ext Js代理传递参数

javascript - $compile 显示未捕获的 TypeError : undefined is not a function

javascript - 检测背景图像大小并应用 div 高度

javascript - 使用 JS 在 Google Analytic 中设置目标

php - AJAX 中最简单的脚本与 JQuery 不起作用

javascript - 通过AJAX发送文件返回 'blob'错误

javascript - laravel中如何根据所选选项刷新页面

javascript - 从 Canvas 中删除元素