【问题标题】:How do I Change font-color in CSS animation using CSS animation如何使用 CSS 动画更改 CSS 动画中的字体颜色
【发布时间】:2022-03-11 04:13:06
【问题描述】:

我正在尝试这样当我打开页面时,test 将显示为红色,testing 将显示为白色。当我想要保留的页面打开时,我使用了延迟(如果您运行程序,您会看到)。

CSS

#hero h1 {
  display: block;
  width: fit-content;
  font-size: 3rem;
  position: relative;
  color: transparent;
  animation: text_reveal .5s ease forwards;
  animation-delay: 1s;
}

#hero h1 .slide {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 0;
  background-color: crimson;
  animation: text_reveal_box 1s ease;
  animation-delay: .5s;
}


/* KetFrames */

@keyframes text_reveal_box {
  50% {
    width: 100%;
    left: 0;
  }
  100% {
    width: 0;
    left: 100%;
  }
}

@keyframes text_reveal {
  100% {
    color: white;
  }
}

HTML

<section id="hero">
  <div class="hero container">
    <div>
      <h1><span class="red">test</span> testing<span class="slide"></span></h1>
    </div>
  </div>
</section>

【问题讨论】:

  • 为什么不让红色类的跨度有颜色:红色;在 CSS 中。我想这就是你想要的
  • 颜色来自文字显示
  • @Naman 似乎是一个简单的解决方案,但是当动画发生时,就会有红色文本坐在那里。
  • 好吧,给 z-index: 10;滑动到所有东西的顶部。
  • @Naman 仍然会有红色文本坐在那里...因为动画设置了延迟。

标签: html css css-animations keyframe


【解决方案1】:

你的意思是这样吗?查看/* added CSS */下的更改

body {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  background-color: grey;
}

html {
  font-size: 20px;
  font-family: 'Montserrat', sans-serif;
}

#hero h1 {
  display: block;
  width: fit-content;
  font-size: 3rem;
  position: relative;
  color: transparent;
  animation: text_reveal .5s ease forwards;
  animation-delay: 1s;
}

#hero h1 .slide {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 0;
  background-color: crimson;
  animation: text_reveal_box 1s ease;
  animation-delay: .5s;
}


/* KetFrames */

@keyframes text_reveal_box {
  50% {
    width: 100%;
    left: 0;
  }
  100% {
    width: 0;
    left: 100%;
  }
}

@keyframes text_reveal {
  100% {
    color: white;
  }
}

/* added CSS */
.red {
  animation: text_reveal_red ease forwards;
  animation-delay: 1s;
  animation-iteration-count: 1;
}

@keyframes text_reveal_red {
  100% {
    color: crimson;
  }
}
<section id="hero">
  <div class="hero container">
    <div>
      <h1><span class="red">test</span> testing<span class="slide"></span></h1>
    </div>
  </div>
</section>

【讨论】:

    猜你喜欢
    • 2015-11-01
    • 1970-01-01
    • 2016-12-13
    • 2012-11-18
    • 2021-11-10
    • 2018-09-25
    • 2015-05-26
    • 1970-01-01
    • 2013-12-18
    相关资源
    最近更新 更多