2012-10-10 45 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