Try using .animate() instead of .css().
$('#foo').animate({'height': '100%'}, 500);
If you want to include some easing
effects, you need to include jQueryUI effects also, and then the code would be:
$('#foo').animate({'height': '100%'}, {easing: 'easeInOutCirc', duration: 500});
EDIT: Alright, then make a class for the element, say, .in-transition
, which would be:
.in-transition{
-webkit-animation: animateHeight 0.5s linear forwards;
-moz-animation: animateHeight 0.5s linear forwards;
-ms-animation: animateHeight 0.5s linear forwards;
-o-animation: animateHeight 0.5s linear forwards;
animation: animateHeight 0.5s linear forwards;
}
@keyframes animateHeight {
0% {
height: (enterCurrentHeight || set it to 0 beforehand);
}
100% {
height: 100%;
}
}
@-webkit-keyframes animateHeight {
0% {
height: (enterCurrentHeight || set it to 0 beforehand);
}
100% {
height: 100%;
}
}
And then you only add/remove that class:
$('#foo').on('click', function(){
var this = $(this);
this.addClass('in-transition'); //or use toggleClass
});
Explanation for (enterCurrentHeight || set it to 0 beforehand)
:
If your #foo
element has some height already when you want to start the animation, you need to set the animation's height
value to that height as a starting point.
e.g. #foo{height: 60px;}
before animation starts.
In that case, animation's keyframes would look like this:
@keyframes animateHeight {
0% {
height: 60px;
}
100% {
height: 100%;
}
}
Otherwise you'd have that jump
effect, where the element's height would go from 60px
to 0 (animation start point)
and then to 100%
.
BUT, if the element's height is 0
beforehand
e.g. #foo{height: 0;}
,
you would set 0
as animation's starting point.
@keyframes animateHeight {
0% {
height: 0;
}
100% {
height: 100%;
}
}
Last solution: Ok, now I get your problem, but you can't solve it using .css(). My suggestion is this, instead of creating and appending the #foo
element on load
, make it directly in your HTML
. As its initial height
is defined as 0
by CSS, it won't be seen. Then you would only add .in-transition
class to it on load
.
Notice:
#(id)
selector beats .(class)
selector, so you need to attach .in-transition
to #foo
, e.g. #foo.in-transition
, very important
HTML:
<div id="foo"></div>
CSS:
#foo{
height: 0;
width: 100px;
background: red;
transition: all 0.5s ease;
-webkit-transition: all 0.5s ease;
}
#foo.in-transition{
height: 100px;
}
JS:
(function(){
var $foo = $('#foo');
$foo.addClass('in-transition');
}
Working example: http://jsfiddle.net/31d57n55/5/
Man, talk about a long answer.