33

Is there a way to animate display:none to display:block using CSS so that the hidden div slides down instead of abruptly appearing, or should I go about this a different way?

$(document).ready(function() {
    $('#box').click(function() {
        $(this).find(".hidden").toggleClass('open');
    });
});
#box {
    height:auto;
    background:#000;
    color:#fff;
    cursor:pointer;
}
.hidden {
    height:200px;
    display:none;
}
    .hidden.open {
        display:block;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="box">
    Initial Content
    <div class="hidden">
        This is hidden content
    </div>
</div>

And a JSFiddle

Sophile
  • 287
  • 3
  • 16
APAD1
  • 13,509
  • 8
  • 43
  • 72
  • 1
    possible duplicate of [Slide down and slide up div on click](http://stackoverflow.com/questions/5279733/slide-down-and-slide-up-div-on-click) – TylerH Jun 19 '14 at 19:25
  • 2
    @TheOneWhoPrograms That question isn't about jQuery or onClick. – TylerH Jun 19 '14 at 19:26

7 Answers7

30

Yes, there is a way: http://jsfiddle.net/6C42Q/12/

By using CSS3 transitions, and manipulate height, rather than display property:

.hidden {
    height: 0px;
    -webkit-transition: height 0.5s linear;
       -moz-transition: height 0.5s linear;
        -ms-transition: height 0.5s linear;
         -o-transition: height 0.5s linear;
            transition: height 0.5s linear;
}

.hidden.open {
     height: 200px;
     -webkit-transition: height 0.5s linear;
        -moz-transition: height 0.5s linear;
         -ms-transition: height 0.5s linear;
          -o-transition: height 0.5s linear;
             transition: height 0.5s linear;
}

More here: Slide down div on click Pure CSS?

Community
  • 1
  • 1
sinisake
  • 11,240
  • 2
  • 19
  • 27
15

Since you're already using jQuery, the simplest thing is just to use slideDown(). http://api.jquery.com/slidedown/

There's also slideToggle().

Then you don't need to manually do all the browser-specific transition css.

bhamlin
  • 5,177
  • 1
  • 22
  • 17
7

I like the idea of CSS transitions, but it's still very jumpy. Sometimes the max-height has to be set to a very high number because of dynamic content which renders the transition useless as it's very jumpy. So, I went back to jQuery, but it had its own faults. inline elements are jumpy.

I found this to work for me:

$(this).find('.p').stop().css('display','block').hide().slideDown();

The stop stops all previous transitions. The css makes sure it's treated as a block element even if it's not. The hide hides that element, but jquery will remember it as a block element. and finally the slideDown shows the element by sliding it down.

Webmaster G
  • 502
  • 5
  • 12
  • Thanks! Exactly what I was looking for :) How can I slide it back up? – Gintas K May 04 '17 at 10:06
  • 1
    Probably use slideUp instead of slideDown I'm guessing. `$(this).find('.p').stop().css('display','block').hide().slideUp();` Or you can slideToggle? `$(this).find('.p').stop().css('display','block').hide().slideToggle();` – Webmaster G May 05 '17 at 12:45
  • Thanks! a simple `.stop().slideUp()` did the job :) – Gintas K May 05 '17 at 13:00
4

What about

$("#yourdiv").animate({height: 'toggle'});

Toggle will switch your div on/off, and the animate should make it appear from below. In this scenario, you don't need the specific CSS to "hide" it.

Martin
  • 7,634
  • 1
  • 20
  • 23
4

We can use visibility: hidden to visibility: visible instead of display: none to display: block property.

See this example:

function toggleSlide () {
  const div = document.querySelector('div')
  
  if (div.classList.contains('open')) {
    div.classList.remove('open')
  } else {
    div.classList.add('open')
  }
}
div {
  visibility: hidden;
  transition: visibility .5s, max-height .5s;
  max-height: 0;
  overflow: hidden;
  
  /* additional style */
  background: grey;
  color: white;
  padding: 0px 12px;
  margin-bottom: 8px;
}

div.open {
  visibility: visible;
  /* Set max-height to something bigger than the box could ever be */
  max-height: 100px;
}
<div>
  <p>First paragraph</p>
  <p>Second paragraph</p>
</div>


<button
  onclick="toggleSlide()"
>
  toggle slide
</button>
Nurul Huda
  • 1,438
  • 14
  • 12
0

I did this workaround for the navigation header in my React site.

This is the regular visible css class

.article-header {
  position: fixed;
  top: 0;
  transition: top 0.2s ease-in-out;
}

This is the class that is attached to the div (when scrolled in my case)

.hidden {
  top: -50px !important;
  transition: top 0.5s ease-in-out;
 }
harsha20599
  • 119
  • 3
  • 11
-1

You can use also

$('#youDiv').slideDown('fast');

or you can tell that the active div goes up then the called one goes down

$('.yourclick').click(function(e) {
       var gett = $(this).(ID);
       $('.youractiveDiv').slideUp('fast', function(){
        $('.'+gett).slideDown(300);
       });
});

Something like that.

Rodrigo Zuluaga
  • 497
  • 3
  • 11