获取之前选择的列表项索引并与当前的进行比较

get previous selected list item index and compare to current

我有一个列表作为轮播的分页点。每个选中的项目都会添加一个选中的 class,我可以轻松获取该列表项的索引。

我需要能够将当前选定的列表项索引与之前选定的列表项索引进行比较,以便确定是否需要向它们添加任何动画。

理想情况下,我希望能够做到以下几点

if(currentIndex > 3 && currentIndex > oldIndex)
{do stuff}

创建一个变量来存储旧索引。选择下一项时,使用该变量的值来执行您想要的操作。完成后,将新位置分配给变量。当用户再次选择时,将是旧索引。

我不知道你的代码,希望你能理解:

var oldIndex = -1; // -1 indicates that the user hasn't yet selected any item
$('.all-the-items').click(function () {
  var currentIndex = $(this).index();
  if (oldIndex === -1) {
    // the user has selected for the first time
  } else {
    // oldIndex holds the old index, currentIndex holds the current index
    // do stuff with oldIndex and currentIndex
    if(currentIndex > 3 && currentIndex > oldIndex) {
      // ...
    }
  }
  oldIndex = currentIndex; // set the old index to the current index
});