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.
The animate() method performs a custom animation of a set of CSS properties. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect. Only numeric values can be animated (like "margin:30px").
jQuery Animations - The animate() Method The jQuery animate() method is used to create custom animations. Syntax: $(selector). animate({params},speed,callback);
The animate() is an inbuilt method in jQuery which is used to change the state of the element with CSS style. This method can also be used to change the CSS property to create the animated effect for the selected element. Syntax: (selector).
JavaScript animations are done by programming gradual changes in an element's style. The changes are called by a timer. When the timer interval is small, the animation looks continuous.
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
Replace comman(,) by colon(:).
$("#full-wrapper #full").animate({ marginLeft: "-=938px" }, 500);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With