代码之家  ›  专栏  ›  技术社区  ›  HandiworkNYC.com

jquery:如何确定next()何时到达末尾,然后转到第一项

  •  28
  • HandiworkNYC.com  · 技术社区  · 15 年前

    我正在使用next()函数显示一系列元素。一旦我到达终点,我想进入第一个元素。有什么想法吗?

    代码如下:

    //Prev / Next Click
    $('.nextSingle').click( function() {
        //Get the height of the next element
        var thisHeight = $(this).parent().parent().parent().next('.newsSingle').attr('rel');
        //Hide the current element
        $(this).parent().parent().parent()
            .animate({
                paddingBottom:'0px',
                top:'48px',
                height: '491px'
            }, 300) 
            //Get the next element and slide it in      
            .next('.newsSingle')
            .animate({
                top:'539px',
                height: thisHeight,
                paddingBottom:'100px'
            }, 300);
    });
    

    基本上,我需要一个“if”语句,表示“如果没有剩余的“next”元素,那么找到第一个。

    谢谢!

    4 回复  |  直到 10 年前
        1
  •  31
  •   user113716    15 年前

    确定 .next() 提前检查 length 财产。

    $('.nextSingle').click( function() {
           // Cache the ancestor
        var $ancestor = $(this).parent().parent().parent();
           // Get the next .newsSingle
        var $next = $ancestor.next('.newsSingle');
           // If there wasn't a next one, go back to the first.
        if( $next.length == 0 ) {
            $next = $ancestor.prevAll('.newsSingle').last();;
        }
    
        //Get the height of the next element
        var thisHeight = $next.attr('rel');
    
        //Hide the current element
        $ancestor.animate({
                paddingBottom:'0px',
                top:'48px',
                height: '491px'
            }, 300);
    
            //Get the next element and slide it in      
        $next.animate({
                top:'539px',
                height: thisHeight,
                paddingBottom:'100px'
            }, 300);
    });
    

    顺便说一下,你可以换一个 .parent().parent().parent() 具有 .closest('.newsSingle') (如果标记允许)。

    编辑: 我纠正了 thisHeight 使用 $next 我们引用的元素。

        2
  •  17
  •   lucuma    12 年前

    作为一个有用的参考,您可以编写并包含以下函数:

    $.fn.nextOrFirst = function(selector)
    {
      var next = this.next(selector);
      return (next.length) ? next : this.prevAll(selector).last();
    };
    
    $.fn.prevOrLast = function(selector)
    {
      var prev = this.prev(selector);
      return (prev.length) ? prev : this.nextAll(selector).last();
    };
    

    而不是:

    var $next = $ancestor.next('.newsSingle');
       // If there wasn't a next one, go back to the first.
    if( $next.length == 0 ) {
        $next = $ancestor.prevAll('.newsSingle').last();;
    }
    

    那将是:

    $next = $ancestor.nextOrFirst('.newsSingle');
    

    参考文献: http://www.mattvanandel.com/999/jquery-nextorfirst-function-guarantees-a-selection/

        3
  •  6
  •   Oren Mazor    15 年前

    根据jquery文档,空的jquery对象将返回.length 0。

    所以你需要做的是在你打电话的时候检查一下退货情况。下一步,然后打电话:首先

    http://api.jquery.com/next/

        4
  •  1
  •   Martijn Wieringa    10 年前

    您可以使用这些函数来查看当前项是否是第一个子项/最后一个子项。

    jQuery.fn.isFirst = function() { return (this[0] === this.parent().children().first()[0]); };
    jQuery.fn.isLast = function() { return (this[0] === this.parent().children().last()[0]); };
    
    if($ancestor.isLast())
    {
        // ...
    }
    else
    {
        // ...
    }