Jquery隐藏前12个元素,显示接下来的12个元素

Vaz*_*lly 2 javascript php jquery

我想要做的是隐藏前12个元素并显示接下来的12个元素.

//this is dynamic loaded content
<div class="inner-content">
     <div class="front-pro">1</div>
     <div class="front-pro">2</div>
     <div class="front-pro">3</div>
     <div class="front-pro">4</div>
     <div class="front-pro">5</div>
     <div class="front-pro">6</div>
     <div class="front-pro">7</div>
     <div class="front-pro">8</div>
     <div class="front-pro">9</div>
     <div class="front-pro">10</div>
     <div class="front-pro">11</div>
     <div class="front-pro">12</div>
     <div class="front-pro hidden">13</div>
     <div class="front-pro hidden">14</div>
     ..... etc (200 divs more)
</div>
<div onclick="SearchNext();">next</div>
Run Code Online (Sandbox Code Playgroud)

这是我的javascript/jquery:

function SearchNext(){
 var first = $('.inner-content').children('.front-pro:hidden:first');
    first.prevAll(':lt(12)').hide();
    first.nextAll(':lt(12)').show();
}
Run Code Online (Sandbox Code Playgroud)

它停止工作后有一次工作.(它跳过了13号)我希望每次下一次点击都能看到12个新元素并隐藏前一个元素.

更新 - 这是我最终的结果,完美的 JSFIDDLE DEMO

感谢Alex Char

用于创建页码的PHP,您也可以使用javascript执行此操作

//$counter is search results
    $x = 1; 
    $Pnumbers = '';
    while($x <= ceil($counter/12)) {
        if($x == 1){ $ecl = 'bold'; } else{ $ecl = ''; }
       $Pnumbers .= ' <span class="number '.$ecl.' numbering" onClick="GoTo('.$x.');">'.$x.'</span> ';
        $x++;
    } 

    if($counter > 12){ echo'<div class="page-numbers">
    <span class="prev number" onclick="GoTo(\'prev\')">Prev</span>
    '.$Pnumbers.'
    <span class="next number" onclick="GoTo(\'next\');">Next</span>
    </div>'; }
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

function GoTo(nn) {
      var nng = parseInt($('.page-numbers .numbering.bold').text());
     if(nn == 'next'){
        nn = nng+1; 
     }if(nn == 'prev'){
         nn = nng-1; 
     } 
    //get all child elements with class front-pro 
    //of element with class .inner-content
    var childElems = $(".inner-content .front-pro");
    var totalpages = Math.ceil(childElems.length/12);
    //iterate through the elements
    var first = (nn-1)*12;
    var last = first+11;
    childElems.each(function(i, el) {

      //show the elements that match the criteria removing css class
      if (i >= first && i <= last) {
        $(el).removeClass('hidden');
      } else {
        //hide rest
        $(el).addClass('hidden');
      }
    });

    if(nn > 1){ $('.page-numbers .prev').show(); }else{ $('.page-numbers .prev').hide(); }
    if(nn < totalpages){ $('.page-numbers .next').show(); }else{ $('.page-numbers .next').hide(); }
    $('.page-numbers .numbering').removeClass('bold');
    $('.page-numbers .numbering:eq('+(nn-1)+')').addClass('bold');
  }
Run Code Online (Sandbox Code Playgroud)

CSS:

.front-pro.hidden{ display:none !important; }
.prev {  display: none; }
.page-numbers .number{
background: #ff0000;  }
.page-numbers{ text-align:center; }
.page-numbers .number.bold{ font-weight:bold; background:#000; }
.page-numbers .number:hover{ background:#000; cursor: pointer; }
Run Code Online (Sandbox Code Playgroud)

确保前12个div不包含"隐藏"类,所有后来的div应该在那里"隐藏"

Ale*_*har 5

我改变了一点实现支持和之前的.我使用css类来隐藏内容.

function searchNext() {
  $('.inner-content').children('.front-pro:lt(12)').addClass('hidden');
  $('.inner-content').children('.front-pro:gt(11)').removeClass('hidden');
  $(".next").hide();
  $(".prev").show();
}

function searchPrev() {
  $('.inner-content').children('.front-pro:lt(12)').removeClass('hidden');
  $('.inner-content').children('.front-pro:gt(11)').addClass('hidden');
  $(".next").show();
  $(".prev").hide();
}
Run Code Online (Sandbox Code Playgroud)
.front-pro.hidden {
  display: none;
}
.prev {
  display: none;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inner-content">
  <div class="front-pro">1</div>
  <div class="front-pro">2</div>
  <div class="front-pro">3</div>
  <div class="front-pro">4</div>
  <div class="front-pro">5</div>
  <div class="front-pro">6</div>
  <div class="front-pro">7</div>
  <div class="front-pro">8</div>
  <div class="front-pro">9</div>
  <div class="front-pro">10</div>
  <div class="front-pro">11</div>
  <div class="front-pro">12</div>
  <div class="front-pro hidden">13</div>
  <div class="front-pro hidden">14</div>
</div>
<div class="next" onclick="searchNext();">next</div>
<div class="prev" onclick="searchPrev();">prev</div>
Run Code Online (Sandbox Code Playgroud)

我在下一个和上一个评论后创建一个通用的解决方案(我使用第3步进行演示,但你可以使用你想要的任何东西):

var pager = (function() {
  /*declare private variables*/
  var first = 0,
    last = 2,
    step = 3;

  function searchNext() {
    //next function
    //increasing first and last variables
    first += step;
    last += step;
    pagerHelper();
  }

  function searchPrev() {
    //previous function
    //decrease first and last variables
    first -= step;
    last -= step;
    pagerHelper();
  }

  function pagerHelper() {
    //get all child elements with class front-pro 
    //of element with class .inner-content
    var childElems = $(".inner-content .front-pro");
    //iterate through the elements
    childElems.each(function(i, el) {
      //show the elements that match the criteria removing css class
      if (i >= first && i <= last) {
        $(el).removeClass('hidden');
      } else {
        //hide rest
        $(el).addClass('hidden');
      }
    });
    nextPreviousToggle(childElems.length);
  }

  function nextPreviousToggle(maxEle) {
    //here the code is to show/hide next/previous buttons
    if (last >= maxEle) {
      $(".next").hide();
    } else {
      $(".next").show();
    }
    if (first === 0) {
      $(".prev").hide();
    } else {
      $(".prev").show();
    }
  }
  return {
    searchNext: searchNext,
    searchPrev: searchPrev
  }
})();
Run Code Online (Sandbox Code Playgroud)
.front-pro.hidden {
  display: none;
}
.prev {
  display: none;
}
.prev:hover,
.next:hover {
  text-decoration: underline;
  cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="inner-content">
  <div class="front-pro">1</div>
  <div class="front-pro">2</div>
  <div class="front-pro">3</div>
  <div class="front-pro hidden">4</div>
  <div class="front-pro hidden">5</div>
  <div class="front-pro hidden">6</div>
  <div class="front-pro hidden">7</div>
  <div class="front-pro hidden">8</div>
  <div class="front-pro hidden">9</div>
  <div class="front-pro hidden">10</div>
  <div class="front-pro hidden">11</div>
  <div class="front-pro hidden">12</div>
  <div class="front-pro hidden">13</div>
  <div class="front-pro hidden">14</div>
</div>
<span class="next" onclick="pager.searchNext();">next</span>
<span class="prev" onclick="pager.searchPrev();">prev</span>
Run Code Online (Sandbox Code Playgroud)

参考

:GT()

:LT()