使用jQuery进行动画处理

时间:2015-10-02 15:07:33

标签: jquery

我正在尝试动画" margin-left"通过" animate()" jQuery,其中" margin-left"的值动态保存在变量中,不是硬编码,如

$('.animate-box').animate({'margin-left': '+=60'}) // value is not hard-coded like this

但更喜欢这样,以便可以动态更改值。

$('.animate-box').animate({'margin-left': +=margin}// like this

但由于某种原因我无法将其设为动画。 Fiddle to the Demonstration

谢谢。

1 个答案:

答案 0 :(得分:0)

两个问题:

  1. 您在选择器中遗漏了一段时间。它应该是var margin = $('.top-box').width();

  2. 其次,您需要将+=括在引号中并将其与值连接。 'margin-left': '+=' + margin

  3. <强> jsFiddle example

相关问题