2015-04-04 80 views
-9

我怎樣才能使這個乘法工作? 3號線.thx乘法百分比和變量

$("div").each(function(i) {     
    $(this).animate({ 
    left: "25%" * i //no idea how to concatenate this 
    },1300)      
+1

「25%」是文字,它是沒有意義的,試圖乘以它 – 2015-04-04 14:48:41

+0

@George Jempty,這就是爲什麼我寫了一個評論efter .... – Noobest 2015-04-04 16:32:56

回答

2
$("div").each(function(i) { 
    var perc = (25 * i) + "%";     
    $(this).animate({ 
    left: perc //no idea how to concatenate this 
    },1300) 

嘗試這種方式

和如果u使用jQuery - 更好地將

$.each($("div"), function(i, v) { 
    var perc = (25 * i) + "%";     
    $(v).animate({ 
    left: perc //no idea how to concatenate this 
    },1300); 
}); 
+0

thx隊友!爲什麼第二種選擇比第一種更好? – Noobest 2015-04-04 13:24:32

+0

$ .each - 每個來自jQuery,它只是用於一個jQuery語法中的代碼視圖 – Legendary 2015-04-04 13:25:15

+0

如果所有的作品 - 不要忘記批准改進SO) – Legendary 2015-04-04 13:26:07

1

試試這個

$("div").each(function(i) {     
    $(this).animate({ 
    left: (25 * i) + '%' 
    },1300) 

希望這有助於