[英]More concise way of writing css/sass keyframe code
我希望模态使用它们出现的动画的反转来隐藏。
到目前为止,我提出的策略是使用“反向”对应物创建 CSS 规则:
.modal-animate-zoom {
animation: animatezoom 0.77s;
}
.modal-animate-zoom-reverse {
animation: animatezoom-reverse 0.77s;
}
@keyframes animatezoom {
from {transform: scale(0)}
to {transform: scale(1)}
}
@keyframes animatezoom-reverse {
from {transform: scale(1)}
to {transform: scale(0)}
}
当我想隐藏模态时,在 JavaScript 中做这样的事情:
modal.classList.remove('modal-animate-popup')
modal.classList.add('modal-animate-popup-reverse')
// modalsList is the children of parent container
setTimeout(_ => { modalsList.removeChild(modal); }, 770)
这有效。 我遇到的问题是:
我正在考虑这两个选项:
任何人都可以推荐任何一种方式或替代解决方案吗?
您可以删除animatezoom-reverse
动画并根据包含的反向类更改animation-direction
。 请注意, forwards
和reverse
做两个完全不同的事情。
目标将保留由执行期间遇到的最后一个关键帧设置的计算值。
动画步骤向后执行,计时功能也反向执行。
.modal-animate-zoom { animation: animatezoom 0.77s forwards; } .modal-animate-zoom-reverse { animation: animatezoom 0.77s reverse forwards; } @keyframes animatezoom { from {transform: scale(0);} to {transform: scale(1);} } .box { width: 100px; height: 100px; background-color: green; }
<div class="box modal-animate-zoom">box</div> <div class="box modal-animate-zoom-reverse">box</div>
上面更精致的版本,重复更少:
.modal-animate-zoom { animation: animatezoom 0.77s forwards; } .reverse-animation { animation-direction: reverse; } @keyframes animatezoom { from {transform: scale(0);} to {transform: scale(1);} } .box { width: 100px; height: 100px; background-color: green; }
<div class="box modal-animate-zoom">box</div> <div class="box modal-animate-zoom reverse-animation">box</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.