I'm trying to animate an element from height: 20px
to height: 0
, but its not working. However, a transition from 0 to 20 does work. I'm using angular to add/remove items from an array:
angular.module('app', [])
.controller('ctl', ctl);
ctl.$inject = ['$timeout'];
function ctl($timeout) {
var self = this;
self.newItemText = '';
self.deleteItem = function(id) {
self.items[id].class = 'hidden';
};
self.addItem = function() {
var newItem = {
id: self.items.length,
class: 'hidden',
text: self.newItemText
};
self.items.push(newItem);
self.items[self.items.length - 1].class = 'visible';
self.newItemText = '';
}
self.items = [{
id: 0,
class: 'visible',
text: 'one'
}, {
id: 1,
class: 'visible',
text: 'two'
}, {
id: 2,
class: 'visible',
text: 'three'
}, {
id: 3,
class: 'visible',
text: 'one'
}, {
id: 4,
class: 'visible',
text: 'two'
}, {
id: 5,
class: 'visible',
text: 'three'
}, {
id: 6,
class: 'visible',
text: 'one'
}, {
id: 7,
class: 'visible',
text: 'two'
}, {
id: 8,
class: 'visible',
text: 'three'
}, ];
};
body {
font-family: arial;
}
.text {
display: inline-block;
}
.close {
cursor: pointer;
}
.visible {
height: 20px;
transition: height 0.2s linear;
}
.hidden {
height: 0;
overflow: hidden;
transition: height 0.2s linear;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<div ng-app='app' ng-controller='ctl as c'>
<input ng-model='c.newItemText' />
<button ng-click='c.addItem()'>
add
</button>
<div>
<ul>
<li ng-repeat='item in c.items' class='{{item.class}}'>
<span class='text'>{{item.text}}</span>
<span class='close' ng-click='c.deleteItem(item.id)'>x</span>
</li>
</ul>
</div>
</div>
relevant css:
.visible {
height: 20px;
transition: height 0.2s linear;
}
.hidden {
height: 0;
overflow: hidden;
transition: height 0.2s linear;
}
full code: