2014-09-04 90 views
2

如何獲取CoffeeScript .each循環的索引?我到處搜索,似乎無法找到一個堅實的答案。我知道如何用vanilla jQuery做到這一點,但我無法弄清楚如何在CoffeeScript中將index參數添加到function()中。coffeescript。每個循環的索引

這裏是目前我的代碼:

video_list_element = $('#video-list li') 

video_list_element.each -> 
    video_list_element.delay(100).animate({ 
     "top": "0" 
}, 2000) 

我試圖用。每圈的指數乘以.delay()內的值

非常感謝你的幫助, 對此,我真的非常感激!!!

問候, 添

回答

0

的文檔jQuery的。每個()函數在這裏找到: http://api.jquery.com/each/

video_list_element = $('#video-list li') 
video_list_element.each (index, element) -> 
    element.delay(100 * index).animate "top": "0", 2000 

一般(SANS-jQuery的),該方式獲得該指數的CoffeeScript的for循環:

array = ["item1", "item2", "item3"] 
for value, index in array 
    console.log index, value 

給出:

0 item1 
1 item2 
2 item3 
+0

我該如何做到這一點,如果我想通過選擇器$('p')遍歷所有的,例如「p」標籤,並使用索引對每個「p」標籤進行動畫處理,以使每次迭代的速度相乘? – staticinteger 2014-09-04 15:31:39

+0

看看我的編輯 – staticinteger 2014-09-04 15:39:04

+0

太棒了!非常感謝你,這很好! – staticinteger 2014-09-04 16:44:28