tags:

views:

26

answers:

1

I have a div box

<div style="width: 400px; height: 400px; position: absolute; left: 350px; top: 350px; background-color: #cdcdcd;"></div>

i can easily animate its width towards right side (left edge will be on fixed position).
How to fix the right edge of the box and animate its width towards left???
i.e. box will expand towards left side.

+3  A: 

You can do it by animating the box's margin-left and width at the same time. Alternatively, if your box is absolutely positioned, animate it's left property and width at the same time.

HTML

<div id="loading"></div>

CSS

#loading {
   margin-left: 200px;
   width: 0;
   height: 10px;
   background: red;
}

jQuery

$('#loading').animate({width: 200, marginLeft: 0}, {duration: 1000});

You can see it in action here.

Pat
there is a jerky effect on right edge
coure06
Easy enough to get rid of with a parent container that's just slightly narrower and has `overflow: hidden;` set: http://jsfiddle.net/nakYV/2/ This covers up the jerkiness caused by jQuery trying to animate 2 properties synchronously. You'll also notice that as you decrease the duration, the jerkiness is less visible since there are fewer steps in the animation that need to match.
Pat
not applicable in my situation, any other solution? i mean i cannot wrap it
coure06
What's your situation? Other options would be to move a background image from right to left or overlay an element on the right edge to cover up jitter.
Pat