0
我有一個列表作爲輪播的分頁點。每個選定的項目添加一個選定的類,我可以很容易地獲得該列表項目的索引。獲取上一個選定的列表項目索引並與當前比較
我需要能夠比較當前選擇的列表項目索引與先前選擇的列表項目索引,以便我可以確定是否需要向他們添加任何動畫。
理想我想在一個位置做如下
if(currentIndex > 3 && currentIndex > oldIndex)
{do stuff}
我有一個列表作爲輪播的分頁點。每個選定的項目添加一個選定的類,我可以很容易地獲得該列表項目的索引。獲取上一個選定的列表項目索引並與當前比較
我需要能夠比較當前選擇的列表項目索引與先前選擇的列表項目索引,以便我可以確定是否需要向他們添加任何動畫。
理想我想在一個位置做如下
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
});
你如何檢索當前的指數? – PeterMader
var currentIndex = $(this).index(); – user1464853
將oldIndex初始化爲-1。做一個檢查;如果oldIndex爲-1,則設置oldIndex = currentIndex,否則,如果currentIndex> oldIndex(在此處做東西......然後設置oldIndex = currentIndex)。 –