I want to animate the appearance and disappearance of table rows.
First of all I tried using a CSS transition, but it did nothing due to the change of the display
property.
So I used an animation, which works as expected.
The problem is, the full height of the row is reserved when the animation begins. See the snippet below for an illustration of the problem: Row 3 is pushed down straight away, before the animation begins.
How can I animate the height of the row progressively, so that it only takes as much space as needed?
And as a bonus:
- it should not require a fixed height for rows
- it should appear as a translation, rather than a scaling; it should look like it's sliding from the bottom of the row above it
- it should be bidirectional (do the opposite when I hide it)
$('button').on('click', function() {
$('tr:nth-child(2)').toggleClass('active');
});
@keyframes anim {
0% {
transform: scaleY(0);
}
100% {
transform: scaleY(1);
}
}
tr {
background: #eee;
border-bottom: 1px solid #ddd;
display: none;
transform-origin: top;
}
tr.active {
display: table-row;
animation: anim 0.5s ease;
}
td {
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<button type="button">Toggle</button>
<table>
<tbody>
<tr class="active">
<td>Row 1</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>Row 2</td>
<td>...</td>
<td>...</td>
</tr>
<tr class="active">
<td>Row 3</td>
<td>...</td>
<td>...</td>
</tr>
</tbody>
</table>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…