2

The documentation of jQuery.animate states that

The only required parameter is a map of CSS properties. This map is similar to the one that can be sent to the .css() method, except that the range of properties is more restrictive.

So, why does this work

$('#con div').css( {
        top : function(i) {
            console.log(i);
            return (i * 500) + 'px';
        }
    }
);

and this doesn't?

$('#con div').animate( {
        top : function(i) {
            console.log(i);
            return (i * 500) + 'px';
        }
    }
);

The console shows that the function is being evaluated for css, but not for animate. Am I missing something?

By the way, I'm using 1.4.2.

2
  • 2
    From reading the doc, it appears that the animate function is restrictive in the css map. It allows only numeric values and some special exceptions. I'm sure you read that too, but I wonder if that means it won't allow a function because it can't check the type. Just speculating. Might be time to look at the code because I can't find anything on this. Commented Apr 7, 2010 at 16:16
  • Agreed -- I just need to find another solution. Commented Apr 7, 2010 at 16:22

1 Answer 1

2

This may not be what you're after, but a simple solution would be simply to iterate over the elements and apply the animations individually:

$('#con div').each(function(i)
{
    $(this).animate({ top: i * 500 });
});

This is essentially what your snippet amounts to, after all.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this works great! I just started with jQuery.animate, and didn't think this would cause simultaneous animations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.