tags:

views:

193

answers:

2

For example, to animate a box from the left, I can set "left" to the negative value of "width", and animate it to zero:

 $('.box').each(function(){
      $(this)
           .css('left', $(this).width()*-1 )
     .animate({'left':0},1000)
     ;
 });

Is there a way to do this without the each function? THIS JQUERY IS TOO VERBOSE!

A: 

Put the function in its own function called animateBox(). Then you can do this:

$('.box').each(animateBox);
Click Upvote
Thanks for the response, but that's not really what I'm asking. I was just trying to see if there's some jQuery magic that I'm not aware of.
Jerph
+1  A: 

I'm not sure why you needed to add the "each" in the first place, because most jQuery functions work on multiple elements simultaneously. This should work:

 $('.box').css('left', $(this).width()*-1 ).animate({'left':0},1000);

EDIT: code doesn't work as mentioned in the comments, because $(this) doesn't refer to the right object. This should fix it:

 var $box = $box.css('left', $box.width()*-1 ).animate({'left':0},1000);
mishac
Doesn't the "this" in your example live in the scope that contains the whole statement, not in the scope of $('.box')?
Jerph