Animation How to - Apply transition to a hidden element which is set to display:none








Question

We would like to know how to apply transition to a hidden element which is set to display:none.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-2.1.0.js'></script>
<style type='text/css'>
#animate_me {<!--from   ww  w  .j a  v a  2s. c om-->
  width: 100px;
  height: 100px;
  background-color: #0F0;
  opacity: 0;
  transition: opacity 1s linear;
}

#animate_me.opacityTransition {
  opacity: 1;
}
</style>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
    $("#display_command").click(function(e) {
        $("#animate_me").toggleClass("opacityTransition");
    });
});//]]>  
</script>
</head>
<body>
  <div id=animate_me>animate_me</div>
  <button id=display_command>Show me</button>
</body>
</html>

The code above is rendered as follows: