Jon Lamer
Jon Lamer

Reputation: 438

Animate an additional margin and prevent starting it from 0px again?

I would like make a cool animation with jQuery, but I do not know, how I can prevent the animation from starting at 0px, when I just want to increase the margin to the left. It always starts from 0px to the value I want. For example, the current margin to the left is about 30px and I want to increase it with additional 7px, I would like to prevent it from starting at 0px to 37px and I want an animation from 30px to 37px.

How can I fix my problem?

My (quite dumb and easy) approach is something like that, but it's unfortunately wrong:

$(bar).animate({
'left':previousOffsetLeft+widthOfLastObj + 'px'
});

Upvotes: 0

Views: 41

Answers (1)

Alex
Alex

Reputation: 10226

you can calculate the current margin and use $(bar).css('margin-left', currentmargin).animate() or use "+=":

$(bar).animate({
'left': "+=" + previousOffsetLeft+widthOfLastObj + 'px'
});

though I cant guess how you calculate those variables since you didnt point out :)

Upvotes: 2

Related Questions