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:
$('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>
Press and hold the Ctrl key while you select the parts of the table that you want to animate. On the Animations tab, in the Advanced Animations group, click Add Animation to open the menu of animation options: To make the shapes enter with an effect, point to Entrance, and then click an effect.
Any object can be animated, making it move or fade in or out of the slide. Google Slides makes it easy to apply these effects to some or all of your slides, adding emphasis or polish to your presentation.
Using transitions is one way to animate your grid rows and columns. If your design adjusts the grid structure to cater to different viewports, as long as the number of rows and columns remain the same throughout, you'll see the rows and columns animate to their new sizes.
As table
row
/cell
don't respect a height
smaller than its content we need to use an inner div
, we can't animate display
and we can't animate a height
set to auto
, so I here show a solution using max-height
.
The trick is to give max-height
a value high enough to enable the highest content.
$('button').on('click', function() {
$('tr:nth-child(2)').toggleClass('active');
});
table {
border-collapse: collapse;
}
tr {
background: #eee;
border-bottom: 1px solid #fff;
}
tr, td {
padding: 0;
}
tr td div {
max-height: 0;
padding: 0 10px;
box-sizing: border-box;
overflow: hidden;
transition: max-height 0.3s, padding 0.3s;
}
tr.active td div {
max-height: 100px;
padding: 10px 10px;
transition: max-height 0.6s, padding 0.6s;
}
<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><div>Row 1</div></td>
<td><div>...</div></td>
<td><div>...</div></td>
</tr>
<tr>
<td><div>Row 2</div></td>
<td><div>...</div></td>
<td><div>...</div></td>
</tr>
<tr class="active">
<td><div>Row 3</div></td>
<td><div>...</div></td>
<td><div>...</div></td>
</tr>
</tbody>
</table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With