Javascript – Animating marginLeft with jQuery

csshtmljavascriptjquery

I can't figure out to animate marginLeft with jQuery. I need it to subtract 938px every time a user clicks on a link, which was working fine when I was using .css(), but I can't figure out how to make it work with .animate().

$("#full-wrapper #full").animate({
    marginLeft, -=938px
}, 500);

Can anyone figure out why this doesn't work? This was my CSS version:

$("#full-wrapper #full").css("marginLeft","-=938px");

I was using CSS3 for animation, but need to make it work in older browsers.

Best Solution

There's a syntax error in your code, as you are passing parameters in an object to animate() you should use : not , to delimit each attribute. Try this:

$("#full-wrapper #full").animate({
    marginLeft: '-=938px'
}, 500);

Example fiddle