我有一个工作动画,该动画从纯CSS开始加载。 opacity
和visibility
的问题在于,即使div隐藏了,它仍然会占用空间。
动画制作完成后,如何使div像display: none
一样消失?
animation
而不是transition
,因为它会在加载时设置动画。
.message.success {
background: #28a745;
color: #fff;
animation: autohide 2s forwards;
padding: 1rem;
}
@keyframes autohide {
0% {
opacity: 1;
}
90% {
opacity: 1;
}
100% {
opacity: 0;
}
}
<div class="message success">
Success!
</div>
This text below is expected to jump up after animation is done.
答案 0 :(得分:1)
您可以使用height
属性来实现此效果:
@keyframes autohide {
0% {
opacity: 1;
}
90% {
opacity: 1;
}
99% {
height: auto;
padding: 1rem;
}
100% {
opacity: 0;
height: 0;
padding: 0;
}
}
height
保持auto
直到动画的结尾(99%),然后在完成时将其设置为0
。
答案 1 :(得分:1)
您可以将最后一个关键帧的填充和字体大小设置为零
.message.success {
background: #28a745;
color: #fff;
animation: autohide 2s forwards;
padding: 1rem;
}
@keyframes autohide {
0% {
opacity: 1;
}
85% {
opacity: 1;
}
95% {
opacity: 0;
padding: 1rem;
font-size: inherit;
}
100% {
padding: 0;
font-size: 0;
opacity: 0;
}
}
<div class="message success">
Success!
</div>
This text below is expected to jump up after animation is done.
答案 2 :(得分:0)
你去了,我想这就是你想要的。
通过将
height
赋予0
与transition
完美搭配
.message.success {
background: #28a745;
color: #fff;
animation: autohide 2s forwards;
transition: height 2s ease;
}
@keyframes autohide {
0% {
opacity: 1;
height: auto;
}
90% {
opacity: 1;
height: auto;
}
100% {
opacity: 0;
height: 0;
}
}
<div class="message success">
Success!
</div>
This text below is expected to jump up after animation is done.
您可以根据要求进行过渡。