我想要为表格行的出现和消失添加动画效果。
首先,我尝试使用 CSS 的 transition 属性,但由于 display 属性的变化,它没有任何效果。
所以我使用了 animation 属性,这也达到了预期的效果。
问题是,在动画开始时会保留行的完整高度。在下面的代码片段中可以看到这个问题的演示:第三行直接向下推移,而动画还没有开始。
如何逐步地动画化行的高度,使其只占用必要的空间呢?
额外的要求是:
- 不需要为行设置固定高度; - 应该看起来像一个翻译,而不是缩放;它应该从上面的行“滑落”; - 它应该是双向的(隐藏时反向进行)。
代码片段如下:
首先,我尝试使用 CSS 的 transition 属性,但由于 display 属性的变化,它没有任何效果。
所以我使用了 animation 属性,这也达到了预期的效果。
问题是,在动画开始时会保留行的完整高度。在下面的代码片段中可以看到这个问题的演示:第三行直接向下推移,而动画还没有开始。
如何逐步地动画化行的高度,使其只占用必要的空间呢?
额外的要求是:
- 不需要为行设置固定高度; - 应该看起来像一个翻译,而不是缩放;它应该从上面的行“滑落”; - 它应该是双向的(隐藏时反向进行)。
代码片段如下:
$('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>