2013-06-19 42 views
0

我似乎無法得到這個jQuery回調爲我工作。有任何想法嗎?順便說一句,請不要擔心變量,我有一個特殊的項目。jQuery的動畫回調不工作

$("#" + circle - 1).animate({ 
    opacity: 0, 
    "margin-top": "50", 
    height: '150px', 
    width: '150px' 
}, 1000, function() { 
    $("#" + circle).animate({ 
     opacity: 1, 
     "margin-top": "50", 
     height: '150px', 
     width: '150px' 
    }, 1000); 
}); 
+2

「不工作」是什麼意思?你能創建一個jsFiddle.net的例子嗎? – j08691

+0

它不會運行回調或主父功能......這在JSFiddle中不起作用,因爲它只是一個更大的程序的一部分。抱歉! :( –

+0

嘗試用marginTop替換「margin-top」:< - 並且不要加引號 –

回答

1

要麼你有$("#" + circle - 1)一個問題,這我不知道你想扣除1從什麼,但我猜你的身份證是一個數字的計算過程中,這將失敗,並NAN"#" + circle - 1如此將其更改爲"#" + (circle - 1)並嘗試:

$("#" + (circle - 1)).animate({ //<- here use() to seperate the number calculation otherwise it will be NAN. 
    opacity: 0, 
    "margin-top": "50", 
    height: '150px', 
    width: '150px' 
}, 1000, function() { 

    $("#" + circle).animate({ 
     opacity: 1, 
     "margin-top": "50", 
     height: '150px', 
     width: '150px' 
    }, 1000); 
}); 
1

對我來說1不是有效的ID,使用item1item2過的實例。像這樣(http://jsfiddle.net/balintbako/XSGN8/):

var circle = 2; 
$("#item" + (circle - 1)).animate({ 
    opacity: 0, 
    "margin-top": "50", 
    height: '150px', 
    width: '150px' 
}, 1000, function() { 
    $("#item" + circle).animate({ 
     opacity: 1, 
     "margin-top": "50", 
     height: '150px', 
     width: '150px' 
    }, 1000); 
}); 
+0

這是兩個單獨的項目。 – Smeegs

+0

我有點錯過:) –

+1

使用數字作爲id是有效的新的html規格。也順便說一句,這是我的答案之前,你的編輯本身。 :) – PSL

1

我會假設你的代碼工作得很好,有什麼不對您的變量。如果$('#'+ circle)沒有找到任何東西,那麼沒有其他事情會發生。這裏是你的代碼工作,稍作修改。

http://jsfiddle.net/qbtDj/1/

$("#mydiv").animate({ 
    opacity: 0, 
    "margin-top": "50", 
    height: '150px', 
    width: '150px' 
}, 1000, function() { 
    $("#mydiv").animate({ 
    opacity: 1, 
    "margin-top": "50", 
    height: '150px', 
    width: '150px' 
}, 1000); 
});