【问题标题】:CSS, how to change the background after a certain time and keep it like that [duplicate]CSS,如何在一段时间后更改背景并保持这种状态[重复]
【发布时间】:2021-01-03 05:02:15
【问题描述】:

当我运行它时,颜色会在 5 秒后改变,但在 5 秒后它会变回白色,但我希望它保持 #1b1b1b

.bg {
  -webkit-animation: change-color 5s; /* Safari 4+ */
  -moz-animation:    change-color 5s; /* Fx 5+ */
  -o-animation:      change-color 5s; /* Opera 12+ */
  animation:         change-color 5s; /* IE 10+ */
}

@-webkit-keyframes change-color {
 from {
        background: White;
 }
 to {
        background: #1b1b1b;
 }
}
(I also use -moz-animation etc)

【问题讨论】:

    标签: html css


    【解决方案1】:

    只需在动画声明中添加forwards

    .bg {
      height: 400px;
      width: 400px;
      -webkit-animation: change-color 5s forwards;
      /* Safari 4+ */
      -moz-animation: change-color 5s forwards;
      /* Fx 5+ */
      -o-animation: change-color 5s forwards;
      /* Opera 12+ */
      animation: change-color 5s forwards;
      /* IE 10+ */
    }
    
    @-webkit-keyframes change-color {
      from {
        background: white;
      }
      to {
        background: #1b1b1b;
      }
    }
    <div class="bg"></div>

    请注意background: White 无效,但background: white 无效。

    【讨论】:

    • 只会添加属性 long-hand 是 animation-fill-mode: forwards;
    • @Timvanderploeg 没问题请不要validate 回答;)
    【解决方案2】:

    使用简单的transition

    body {
        background: red;
        transition: background 5s linear;
    }
    
    .recolor {
        background: black;
    }
    

    recolor 类添加到body 会将颜色从red 更改为black

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 2011-06-16
      • 2011-09-24
      • 2023-03-28
      • 2012-08-06
      • 2019-09-20
      • 2023-03-14
      相关资源
      最近更新 更多