9

I have a message box which I want to slide down on click. I do this by adding a css class through Angular (and jQuery in my example). But my CSS transition does not take effect.

Is there any obvious mistake I'm doing?

Here's my fiddle: http://jsfiddle.net/mBKXn/

and my code:

// jQuery
$('.test').on('click',function(){
  $('#msgContainer').toggleClass('msgShow');
});

// HTML
<div class="container">
    <div id="msgContainer" class="msg">
        <p>Message here</p>
        <p>T2</p>
        <p>T4</p>
    </div>
    Test text
</div>

<button class="test">Click</button>

// CSS
.container{
    position: relative;
    height: 200px;
    width: 400px;
    border: solid 1px #222;
}

.msg{
    position: absolute;
    top: 0;
    background-color: #FEEFB3;
    height: 0;
    width: 100%;
    overflow: hidden;
    -webkit-transition: height 0.8s linear;
    -moz-transition: height 0.8s linear;
    -o-transition: height 0.8s linear;
    -ms-transition: height 0.8s linear;
    transition: height 0.8s linear;    
}

.msgShow{
    height: auto;
}
2

3 Answers 3

6

you need to set a defined height. Height:auto won't work as this is the default height value.

see the specs for the height property here: http://www.w3.org/TR/CSS2/visudet.html#the-height-property

http://jsfiddle.net/mBKXn/7/

.msgShow{
    height: 100%;
}
2
  • you could set the height to 100% Commented Jan 15, 2014 at 19:53
  • I tried, but it filled the entire div. But, if I set a max.height, using 100% works perfectly :) Fiddle: jsfiddle.net/mBKXn/8 Commented Jan 15, 2014 at 19:55
6

To animate height from 0 to auto you have to use max-height instead:

.msg{
    position: absolute;
    top: 0;
    background-color: #FEEFB3;
    max-height: 0;
    width: 100%;
    overflow: hidden;
    -webkit-transition: max-height 0.8s linear;
    -moz-transition: max-height 0.8s linear;
    -o-transition: max-height 0.8s linear;
    -ms-transition: max-height 0.8s linear;
    transition: max-height 0.8s linear;    
}

.msgShow{
    max-height: 1000px;
}

Seems to work: http://jsfiddle.net/mBKXn/3/

Also take a look at this question.

12
  • 4
    This does work, but it's problematic because once the max-height value exceeds the height, there are no further visual changes even though the browser continues "animating". When you click again, there's a noticeable pause while the value animates down to the actual height. Commented Jan 15, 2014 at 19:50
  • 1
    max-height: 100%; should be enough ;) jsfiddle.net/mBKXn/6 But Pointy makes a good point Commented Jan 15, 2014 at 19:50
  • @A.Wolff yes but that's 100% of the parent element height, so you still get the pause when toggling back to 0. Commented Jan 15, 2014 at 19:52
  • @Pointy ya, that's correct and can be an issue. Do you know how to solve it? Commented Jan 15, 2014 at 19:53
  • Yes, it's true. So when using this method, one should set reasonable max-height (not 1000px like in my example) to reduce animation delay on back toggle. Commented Jan 15, 2014 at 19:58
3

Another (older IE compliant) way to do this is through slideToggle.

Updated Fiddle that works and another Fiddle where I removed some of your transition css and it makes the animation smoother in my opinion.

your code needs a slight change:

$('.test').on('click',function(){
  $('#msgContainer').slideToggle('slow');
});

and your class needs a slight change:

.msg{
    display:none;
    position: absolute;
    top: 0;
    background-color: #FEEFB3;
    height: auto;
    width: 100%;
    overflow: hidden;
}
2
  • 2
    He's doing a css transition from height 200px to height auto (auto is the cause) Commented Jan 15, 2014 at 19:49
  • Wrong. I'm using CSS transition which is the animation. As both Kevin and Kasper points out, css transition does not work with auto height. Commented Jan 15, 2014 at 19:52

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.