代码之家  ›  专栏  ›  技术社区  ›  YaBCK

.next()和。prev()循环通过div

  •  0
  • YaBCK  · 技术社区  · 9 年前

    我想使用简单的jQuery来执行滑动浏览 div 元素。但是我该如何使用 next() 和 prev() 浏览幻灯片项目?

    问题是你什么时候下一个,下一个,下一个。这个 部门 最后停下来,没有出现;它不会循环回到第一个 部门 .

    这是我目前得到的:

    $("p.right").click(function() {
      $(".item.active").removeClass("active").next(".item").addClass("active").show();
    });
    $("p.left").click(function() {
      $(".item.active").removeClass("active").prev(".item").addClass("active").show();
    });
    Section {
      position: relative;
      height: 200px;
      width: 200px;
      text-align: center;
    }
    
    p {
      position: absolute;
      margin: 0px;
      padding: 5px 10px;
      background: blue;
      color: #ffffff;
      border-radius: 100%;
    }
    
    p.left {
      top: 0px;
      left: 20px;
    }
    
    p.right {
      top: 0px;
      right: 20px;
    }
    
    div {
      display: none;
    }
    
    div.active {
      display: block;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <section>
      <div class="item active">First Item</div>
      <div class="item">Second Item</div>
      <div class="item">Third Item</div>
      <div class="item">Last Item</div>
      <p class="right">N</p>
      <p class="left">P</p>
    </section>
    5 回复  |  直到 9 年前
        1
  •  5
  •   Erik Grosskurth    9 年前

    试着重复一下:

    if (this.is('div:last-child')) { 
       //Start carousel over at first child
    }else {
       // On to the next one
    }
    
        2
  •  3
  •   YaBCK    9 年前

    好好想想,我终于自己找到了答案。如果有人想知道我是怎么做到的,就给你。

    我的灵感来自 Erik Grosskurth 邮递

    $("p.right").click(function(){
    		if ($(".item.active").next(".item").length){
    			$(".item.active").removeClass("active").next(".item").addClass("active");
    		} else {
    			$(".item.active").removeClass("active");
    			$(".item:first").addClass("active");
    		}
    	});
    	
    	$("p.left").click(function(){
    		if ($(".item.active").prev(".item").length){
    			$(".item.active").removeClass("active").prev(".item").addClass("active");
    		} else {
    			$(".item.active").removeClass("active");
    			$(".item").last().addClass("active");
    		}
    	});
    Section{
    position: relative;
    height: 200px;
    width: 200px;
    text-align: center;
    }
    p{
    position: absolute;
    margin: 0px;
    padding: 5px 10px;
    background: blue;
    color: #ffffff;
    border-radius: 100%;
    }
    p.left{
      top: 0px;
      left: 20px;
    }
    p.right{
      top: 0px;
      right: 20px;
    }
    
    div{
     display: none;
    }
    div.active{
     display: block;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <section>
      <div class="item active">First Item</div>
      <div class="item">Second Item</div>
      <div class="item">Third Item</div>
      <div class="item">Last Item</div>
      <p class="right">N</p>
      <p class="left">P</p>
    </section>
        3
  •  1
  •   GreyRoofPigeon    9 年前

    您需要检查 .active 是第一项或最后一项。

    $(function() {
      var numSlides = $('div.active').parent().find('div').length;
    
      $("p.right").click(function() {
        if( $('div.active').index()+1 == numSlides ) {
          $('div.active').removeClass('active').parent().find('div:first').addClass('active');
        }
        else {
          $("div.active").removeClass("active").next("div").addClass("active");
        }
      });
      $("p.left").click(function() {
        if( $('div.active').index() == 0 ) {
          $('div.active').removeClass('active').parent().find('div:last').addClass('active');
        }
        else {
          $("div.active").removeClass("active").prev("div").addClass("active").show();
        }
      });
    });
    Section {
      position: relative;
      height: 200px;
      width: 200px;
      text-align: center;
    }
    
    p {
      position: absolute;
      margin: 0px;
      padding: 5px 10px;
      background: blue;
      color: #ffffff;
      border-radius: 100%;
    }
    
    p.left {
      top: 0px;
      left: 20px;
    }
    
    p.right {
      top: 0px;
      right: 20px;
    }
    
    div {
      display: none;
    }
    
    div.active {
      display: block;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <section>
      <div class="active">First Item</div>
      <div>Second Item</div>
      <div>Third Item</div>
      <div>Last Item</div>
      <p class="right">N</p>
      <p class="left">P</p>
    </section>
        4
  •  1
  •   freginold    9 年前

    下面是一个可行的解决方案,当您到达项目列表的任意一端时,可以进行循环 p.left 由于SO代码段的编码方式(使用 <div> s) 。

    这个 p.right 功能按预期工作 p、 左 在“正常”的web环境中,该函数的工作原理类似。

    $("p.right").click(function() {
      if ($("div.active").next("div").text() == "") {
        // if at the last item, loop around
        $("div.active").removeClass("active");
        $("div").first().addClass("active").show();
      } else {
        // else go to the next one
        $("div.active").removeClass("active").next("div").addClass("active").show();
      }
    });
    $("p.left").click(function() {
      if ($("div.active").prev("div").text() == "") {
        // if at the first item, loop around
        $("div.active").removeClass("active");
        $("div").last().addClass("active").show();
      } else {
        // else go to the previous one
        $("div.active").removeClass("active").prev("div").addClass("active").show();
      }
    });
    Section {
      position: relative;
      height: 200px;
      width: 200px;
      text-align: center;
    }
    
    p {
      position: absolute;
      margin: 0px;
      padding: 5px 10px;
      background: blue;
      color: #ffffff;
      border-radius: 100%;
    }
    
    p.left {
      top: 0px;
      left: 20px;
    }
    
    p.right {
      top: 0px;
      right: 20px;
    }
    
    div {
      display: none;
    }
    
    div.active {
      display: block;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <section>
      <div class="active">First Item</div>
      <div>Second Item</div>
      <div>Third Item</div>
      <div>Last Item</div>
      <p class="right">N</p>
      <p class="left">P</p>
    </section>
        5
  •  0
  •   BeepBop    9 年前

    我有一个部分解决方案来检测您是否正在通过最后一个div(或在第一个div之前):

    https://jsfiddle.net/cewqotsv/1/

    本质上,您可以检测在“活动”div之后是否有任何div

    if($("div.active").next("div").length == 0){
      //do nothing
    }