【问题标题】:Animate background between original and set color在原始颜色和设置颜色之间设置动画背景
【发布时间】:2018-06-10 03:26:41
【问题描述】:

我有随机初始背景颜色的 HTML 元素。

我需要在原始(初始)和新元素之间设置所选元素的背景颜色。

我一直在尝试使用:

@keyframes blink-red {    
    0% { background-color: initial; color: initial; }
    100% { background-color: red; color: #000; }
}

但它似乎忽略了“初始”设置。

我知道它可以用 JavaScript 通过探测初始颜色来完成,但我很想用纯 css 来完成。

密码笔:https://codepen.io/kheim/pen/GGrMoj

谢谢!

【问题讨论】:

    标签: css animation


    【解决方案1】:

    您可以省略具有initial 值的关键帧:

    .animate {
      padding: 1cm;
      animation: blink 3s infinite;
    }
    
    @keyframes blink {
      50% {
        background: green;
        color: #000;
      }
    }
    <div style="background: blue; color:#fff" class="animate">
    Blue background
    </div>
    <div style="background: red; color:#fff" class="animate">
    Blue background
    </div>

    【讨论】:

    • 天啊 - 我不知道这是可能的 - 非常感谢!
    • @user1038334,乐于助人!
    猜你喜欢
    • 2022-12-23
    • 1970-01-01
    • 2019-08-08
    • 2011-02-11
    • 2016-01-02
    • 2015-04-06
    • 1970-01-01
    • 2010-09-17
    • 2018-07-15
    相关资源
    最近更新 更多