2012-10-10 40 views
0

所以,我有一個可以動畫的對象(讓我們稱之爲obj1)。其他對象可以在動畫之前或之後動態創建,所以我希望它們的邊距左邊位置基於obj1的當前邊距左邊。jquery動畫對象,然後創建其他對象基於新的margin-left

這是相關的代碼片段。奇怪的是它與警報一起工作,但是如果我拿出警報,則餘量將默認爲最初在樣式表中的內容。超時是爲了確保新obj不會創建,直到前一個obj完成動畫到新位置爲止。

if(!rounds.match12) { 
    setTimeout(function() { 
    createMatchup(matchups[6].winner, matchups[7].winner, 12, false); //this creates a new matchup and appendsTo current container 
    var pos = $('#matchup5').css('margin-left'); 
    alert(pos); 
    $('#matchup12').css('margin-left' , pos); 
    alert($('#matchup12').css('margin-left')); 
    $('#matchup12').css('margin-left' , '-=195'); 
    alert($('#matchup12').css('margin-left')); 
    rounds.match12 = true; 
    },1500); 
} 

回答

0

沒有看到完整的代碼,我的猜測是你的超時很可能不是與動畫同步發射。 jQuery動畫並不總是精確的,它與你的超時之間的毫秒差異,它可能會失敗或提供不一致的值。嘗試使用超時的$.animate()回調,而不是如下:

// replace 'fast' with a millisecond timeout if desired // 
$('selector').animate(marginLeft: 'X', 'fast', function(){ 
    // dynamically create and position new elements here 
}); 

我希望這有助於!

+0

不幸的是,這對我不起作用。動畫是一種可以應用於多種事情的自己的功能。新對象的創建不一定與動畫相關。我真的只想讓剩餘的新對象邊距基於剩下的其他對象。 – user1310774

+0

@ user1310774你能用你的代碼在jsfiddle.net上創建一個小提琴,因爲這有助於我們提供答案。 – dSquared

+0

我有點修復它我有css過渡包含元素,似乎然後與某些原因改變邊際衝突。 – user1310774