简体   繁体   English

保留CSS动画中的最后一个关键帧属性

[英]Preserve the last keyframe property in CSS animations

I have a div with some text: 我有一些文本的div:

<div>Rain.js</div>

and its CSS: 及其CSS:

div {
  -webkit-animation: fadein 2s;
  color:black;
  font-size:70px;
}

I want to make the div fade in so I made an animation fadein to change the opacity of the div from 0 to 0.1 我想使div淡入,所以我制作了一个动画fadein以将divopacity从0更改为0.1

@keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 0.1; }
}

As you can see here the fadein from opacity 0 to 0.1 occurs in 2 seconds as expected. 正如你可以看到这里fadeinopacity 0至0.1发生在2秒内如预期。 But after 2 seconds the opacity goes from 0.1 to 1. 但是2秒钟后,不透明度从0.1变为1。

1) Why does this happen? 1)为什么会这样?

2) How can I prevent this from happening? 2)如何防止这种情况发生?

When the animation ends it puts the div in his opacity default state, which is not set. 当动画结束,它把div在他的opacity默认状态,即未设置。 (therefore it will be 1 ). (因此它将是1 )。

Add to your CSS on the div : div上添加到CSS:

opacity: 0.1;

Demo: http://jsfiddle.net/qm3f6717/1/ 演示: http//jsfiddle.net/qm3f6717/1/

You have not added animation-fill-mode: forwards to preserve the final state of the animation. 您尚未添加animation-fill-mode: forwards以保留动画的最终状态。

Since there are no default properties set for opacity in your CSS rules and the animation-fill-mode by default is none, the element takes the default opacity value of 1 after the animation is finished. 由于CSS规则中没有为不透明度设置默认属性,并且animation-fill-mode默认情况下为none,因此动画结束后,该元素的默认不透明度值为1。

JSfiddle Demo JSfiddle演示

 div { animation: fadein 2s; color: black; font-size: 70px; animation-fill-mode: forwards; } @keyframes fadein { from { opacity: 0; } to { opacity: 0.1; } } 
 <div>Rain.js</div> 

After the fadein is done, the div takes on its specified values. 淡入完成后,div将采用其指定值。

You simply need to make the opacity of the div equal to the final key frame opacity value. 您只需要使div的不透明度等于最终关键帧的不透明度值即可。

div
{
    -webkit-animation: fadein 2s;
    color:black;
    font-size:70px;
    opacity:0.1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM