简体   繁体   English

使用Jquery在数组中显示以前的项目

[英]Show previous items in array with Jquery

I have this script that shows the next 16 items in the array. 我有这个脚本,显示数组中的下16个项目。 But i want it to show the previous 16 when the "back" button is clicked. 但我希望它显示前一个16点击“后退”按钮。

This is the script i'm using for the "Next" button: 这是我用于“下一步”按钮的脚本:

 var YearList = $('.dateListItem li').toArray(); var index = 0; displayNext(); $(".arrowRight").click(function() { // display next elements displayNext(); }); function displayNext() { $(YearList).hide(); var list = $('.dateListItem'); var index = list.data('index') % YearList.length || 0; list.data('index', index + 2); $(YearList.slice(index, index + 2)).show(); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#" class="arrowRight">NEXT</a> <ul class="dateListItem"> <li>1978</li> <li>1979</li> <li>1980</li> <li>1981</li> <li>1982</li> <li>1983</li> </ul> 

So now i just need it to go the other way as well. 所以现在我只需要它去另一个方向。

Thank you for any assistance. 谢谢你的帮助。

You can replace this js code with yours, 你可以用你的js代码替换

var YearList = $('.dateListItem li').toArray();

var index = 0;

displayNext();

$(".arrowRight").click(function() {
  // display next elements
  displayNext();
});

$(".arrowLeft").click(function() {
  // display next elements
  displayPrev();
});

function displayNext() {
  $(YearList).hide();

  var list = $('.dateListItem'); 
  var index = list.data('index') % YearList.length || 0;

  list.data('index', index + 16);             
  $(YearList.slice(index, index + 16)).show(); 
}

function displayPrev() {
  $(YearList).hide();

  var list = $('.dateListItem'); 
  var index = list.data('index') % YearList.length || 0;
  index = index - 16;
  list.data('index', index);             
  $(YearList.slice(index - 16, index)).show(); 
}

And working jsfiddle here it is. 并在这里工作jsfiddle

You can achieve the previous with slight modification in the function. 您可以通过稍微修改功能来实现之前的功能。 Fiddle Link : jsfiddle 小提琴链接: jsfiddle

var YearList = $('.dateListItem li');

var index = 0;

displayNext();

$(".arrowLeft").click(function() {
  // display next elements
  displayPrevious();
});

$(".arrowRight").click(function() {
  // display next elements
  displayNext();
});

function displayNext() {
  $(YearList).hide();

  var list = $('.dateListItem'); 
  var index = list.data('index') % YearList.length || 0;   

   list.data('index', index+2);  
  $(YearList.slice(index, index + 2)).show(); 
}

function displayPrevious() {
  $(YearList).hide();

  var list = $('.dateListItem'); 
  var pindex = list.data('pindex') % YearList.length || 0;   
  var YearListLength = YearList.length;
  list.data('pindex', pindex+2);  
  $(YearList.slice(YearListLength-2-pindex, YearListLength-pindex)).show(); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM