views:

1015

answers:

4

In the jQuery example below, I have one div inside another. When I animate the inner div down to a width of 0, the outer div (which has absolute positioning), decreases in width along with it.

This is desired.

The trouble is that after the animation is complete, the outer div pops back to its original size. Is this expected? How can I keep this from happening?

Thanks!

Example

html:

<div class="outer"><div class="inner">innerContent</div></div>


css:

div.outer {
    position: absolute;
    padding: 10px;
    background: purple;
}

div.inner {
    position: relative;
    padding-left: 5px;
    padding-right: 5px;
    background: orange;
    clip: auto; overflow: hidden;
}


javascript:

$('.outer').click(function() { 
 $('.inner').animate({width: 0, paddingLeft: 0, paddingRight: 0}, 'slow');
});
+1  A: 

Have you tried setting the outerbox width to 0 after the animation is complete?

$('.inner').animate({width: 0, paddingLeft: 0, paddingRight: 0}, 'slow', function() {
  $('.outer').css("width", 0);
});
peirix
Well, this sort of works. It actually creates a stutter where the outer div still expands to full size for a second before it reduces to 0.
patrick dw
A: 

For me in Firefox 3.0.10 using jQuery 1.3.2 in Ubuntu 9.04 this works perfectly. May be it's some kind of interaction with other html elements in your page?

Keeper
I am using a webkit based browser. Just tried Firefox on Mac OS X, and it works perfectly, so it must be a webkit issue.The example I posted is an extremely simplified version of my page. The behavior is identical whether or not there are other elements.
patrick dw
A: 

In my tests, this works fine in IE, FF and Opera, but produces the bug you mention in Chrome and Safari.

ichiban
Yes, looks like webkit is the culprit. Hoping I can find a suitable workaround.
patrick dw
+3  A: 

Looks like if I grab the width of the 'outer' div, and the outerWidth of the 'inner' div, then subtract the 'outer' div's width from the 'inner' div's outerWidth, and animate the 'outer' width to the result simultaneously while animating the 'inner' to 0, it works.

Any opinions on whether this should be a bug fix request for jquery or webkit or both.

$('.outer').click(function() { 
 var innerWidth = $('.inner').outerWidth();
 var outerWidth = $('.outer').width();
 var theWidth = outerWidth - innerWidth;
 $('.inner').animate({width: 0, paddingLeft: 0, paddingRight: 0}, 'slow' );
 $('.outer').animate({width: theWidth}, 'slow');
});
patrick dw
I'd report it as a jQuery bug. The point of jQuery is to normalize these things between browsers.
Nosredna
By the way, in this example I could just animate the outer to 0, but in my actual page, there is additional content along side the inner div that must remain visible, so reducing the outer to 0 is not actually an option.
patrick dw
Nosredna, good point. Just a hunch that it would probably result in a faster fix as well.
patrick dw
Since you solved your own issue, you should go ahead and accept your own answer.
ichiban
Yes, just waiting out the 48 hours required for accepting my own answer. But thanks for the reminder.
patrick dw