1

I'm trying to animate the height property of an element and for some reason it isn't animating at all.

Here's the fiddle where I'm trying to animate.

HTML

<ul>
    <li>
        li 1
    </li>
    <li>
        li 2
    </li>
    <li>
        li 3
    </li>
</ul>​

Css

ul.hide {
    height: 0px;
    overflow: hidden;
}
ul {
    -webkit-transition: all 1s ease-in-out;
    -moz-transition: all 1s ease-in-out;
    -o-transition: all 1s ease-in-out;
    -ms-transition: all 1s ease-in-out;
    transition: all 1s ease-in-out;
}

JS:

setTimeout(function () { $('ul').addClass('hide'); }, 2000);
setTimeout(function () { $('ul').removeClass('hide'); }, 4000);​

Is there something I'm missing or forgetting about?

qwertymk
  • 34,200
  • 28
  • 121
  • 184

2 Answers2

3

I don't think you can animate height if it's automatic, ie if you're not setting it explicitly. Just try adding height: 50px; to ul in your fiddle.

Use transform: scaleY(0); instead!

dain
  • 6,475
  • 1
  • 38
  • 47
1

See http://jsfiddle.net/uJCQV/1/

You can use

$('ul').css('height',$('ul').height())
setTimeout(function () {$('ul').addClass('hide');}, 2000);
setTimeout(function () { $('ul').removeClass('hide'); }, 4000);

Or you can also use max-height:

ul.hide {
    max-height: 0px;
    overflow: hidden;
}
ul{max-height:999px;}

http://jsfiddle.net/uJCQV/2/

Oriol
  • 274,082
  • 63
  • 437
  • 513