css - 应用后如何反转关键帧
问题描述
这是我正在使用的动画和单击特定按钮时添加相应类的 javascript 代码。它工作正常。
$(document).ready(() => {
$('.show').on('click', () => {
$('.child').addClass('show');
})
$('.hide').on('click', () => {
$('.child').addClass('hide');
})
})
.child{
width:0;
height:0;
border:2px solid red;
overflow:hidden;
}
.child.show{
animation-name: expandit;
animation-duration: 1s;
width:100px;
height:100px;
}
.child.hide{
animation-name: hideit;
animation-duration: 1s;
animation-direction: reverse;
}
@keyframes expandit {
0% {
width: 0;
}
100% {
width: 100px;
}
}
@keyframes hideit {
0% {
width: 0;
}
100% {
width: 100px;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="parent">
<a class="show" href="#">Show</a>
<a class="hide" href="#">Hide</a>
<div class="child">
</div>
</div>
现在用户点击关闭按钮。我想0
通过使用关键帧恢复宽度。我怎样才能做到这一点?
谁能帮我?
解决方案
您正在寻找animation-fill-mode
允许您保持应用动画的最终状态的属性,如w3c 中记录的那样。
$(document).ready(() => {
$('.show').on('click', () => {
$('.child').addClass('show');
})
$('.hide').on('click', () => {
$('.child').addClass('hide');
})
})
.child {
width: 0;
height: 0;
border: 2px solid red;
overflow: hidden;
}
.child.show {
animation-name: expandit;
animation-duration: 1s;
width: 100px;
height: 100px;
}
.child.hide {
animation-name: hideit;
animation-duration: 1s;
animation-direction: reverse;
animation-fill-mode: forwards;
}
@keyframes expandit {
0% {
width: 0;
}
100% {
width: 100px;
}
}
@keyframes hideit {
0% {
width: 0;
}
100% {
width: 100px;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="parent">
<a class="show" href="#">Show</a>
<a class="hide" href="#">Hide</a>
<div class="child">
</div>
</div>
但是,如果您想恢复到“真正的初始状态” where height: 0;
,则需要一个 99.99% 的附加关键帧来隐藏第二个动画。如果您有任何后续问题,请随时发表评论。
推荐阅读
- mongodb - 嵌套对象内的 MongoDB 查询
- html - Angular Accordion 可访问性问题
- c# - 无法访问已处置的对象。对象名称:'tlsxyz'
- java - Google Cloud Search 数据库连接器问题
- angular - 如果你不取消订阅一个守卫的 canActivate 中的 Observable 会发生什么?
- sql - Firebird 3 中一周最后一天的日期
- android - 是否可以仅显示软键中显示的过滤字符?
- wordpress - get_the_terms 在自定义插件中不起作用
- node.js - 如何在 nuxt js 中使用 vuetify 作为插件?
- javascript - 使用选择库时如何设置下拉菜单的默认选项?