开发者

jQuery animation using variable

开发者 https://www.devze.com 2023-01-18 00:39 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消