jQuery animation using variable
I have written a script to slide a div horizontally and stop at a specific margin-left value.
The margin left depends on the link which was clicked, so I have used a calculation to find that specific value. When i try to pass the variable into the .animate({"margin-left":value},"slow");
Here is the code
var marginLeft = parseInt((linkClick * 995)-995)+"px";
$("div_to_animate").animate({
"margin-left":marginLeft},"slow");
I have used the code below to ensure that it is returning the correct va开发者_JS百科lue
alert(marginLeft);
Any suggestions?
Thanks
The CSS selector you use in your example code isn't correct. If your HTML looks like this:
<div id="div_to_animate">...</div>
Your selector should be:
$("#div_to_animate").animate({"margin-left":marginLeft},"slow");
Problem solved, I needed to add a "-" symbol before the marginLeft variable to make it a negative margin.
Thanks for the advice.
精彩评论