使用 "natural"属性值的 CSS3 动画

标签 css animation

我想定义一个 CSS3 动画,它在动画过程中的某些时刻使用属性的自然值,就像未应用动画一样。

例如

@keyframes fadeblue
{
  0%
  {
    background-color: natural;
  }
  100% 
  {
    background-color: blue;
  }
}    
.thing1
{
  background-color: red;
  animation: fadeblue 2s;
}
.thing2
{
  background-color: green;
  animation: fadeblue 2s;
}

thing1 将从红色渐变为蓝色,而 thing2 将从绿色渐变为蓝色。 我应该使用什么值来代替 0% 关键帧中的 natural

我尝试了继承和透明,但都没有达到预期的效果。

注意我知道这可以通过 JavaScript 解决方案来完成,但如果可能的话,我更喜欢纯 css3 解决方案。

最佳答案

看来您无法在关键帧中引用原始颜色。但是,您可以只指定 one keyframekeyframes 声明中,让浏览器为您插入颜色。仅使用 50% 的关键帧将使用 0%(又名 from)和 100% 处的原始属性(又名)。

有了这些知识,我们还可以使用 animation-delay 有效地对动画进行排队,以创建看起来像单个动画的动画,但实际上并非如此。

例如:

@keyframes fadeblue {
  50% {
    background-color: blue;
  }
}
@keyframes fadewhite {
  50% {
    background-color: white;
  }
}   
.thing1 {
  background-color: red;
  animation: fadeblue 2s,
             fadewhite 2s 2s; 
             /* shorthand here is: animation-name animation-duration animation-delay */
}
.thing2 {
  background-color: green;
  animation: fadeblue 2s,
             fadewhite 2s 2s;
}
.thing3 {
  background-color: yellow;
  animation: fadeblue 2s,
             fadewhite 2s 2s;
}
.thing4 {
  background-color: purple;
  animation: fadeblue 2s,
             fadewhite 2s 2s;
}
<div class="thing1">Thing 1</div>
<div class="thing2">Thing 2</div>
<div class="thing3">Thing 2</div>
<div class="thing4">Thing 2</div>

您会看到元素褪色为蓝色,然后恢复为原始颜色,然后褪色为白色,然后恢复为原始颜色。

jsfiddle为了更好的衡量。

关于使用 "natural"属性值的 CSS3 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31023665/

相关文章:

asp.net - 如何让gridview占面板宽度的100%

javascript - 使用单选按钮更改 DIV CSS 类

html - 如何让按钮中的文字垂直居中?

javascript - :not function 的目标元素

javascript - 如何向前动画,等待 x 秒,向后动画,然后删除类?

ios - Swift - 如何制作自定义幻灯片动画?

css - CSS3 动画会造成内存泄漏吗?

jquery - 多个页面网站上的同一页面子导航

android - 如何为 ImageView 的 ImageAlpha 设置动画

iphone - iOS:如何在两个移动的物体之间画一条线?