如何在CSS中制作多步骤简单动画?

问题描述 投票:2回答:2

我想在屏幕中心使用CSS实现以下动画:

  1. 文本“Hello”淡入
  2. 文本“Hello”淡出
  3. 出现小圆圈并更改为更大的矩形

我实施了第三步如下:

.step3 {
  height: 250px;
  width: 250px;
  margin: 0 auto;
  background-color: blue;
  animation-name: stretch;
  animation-duration: 10s;
  animation-timing-function: ease-out;
  animation-delay: 0;
  animation-direction: alternate;
  animation-iteration-count: 1;
  animation-fill-mode: none;
  animation-play-state: running;
}

@keyframes stretch {
  0% {
    transform: scale(.3);
    border-radius: 100%;
  }
  100% {
    transform: scale(1.5);
  }
}
<div class="step3"></div>

我正在阅读有关animate.css的内容,可用于淡入和淡出文本:

<div class="animated fadeIn 1">Hello</div>

但是我怎么能顺序淡出这个文本呢?如何将所有3个步骤放入序列中?

html css css-animations keyframe animate.css
2个回答
2
投票

你需要在这里阅读两个属性1.动画延迟2.动画填充模式

动画延迟会导致动画在一段时间后启动,所以基本上你可以在第一个结束后开始第二个动画

动画填充模式,你可以在这里阅读https://developer.mozilla.org/en-US/docs/Web/CSS/animation-fill-mode

div {
  position: absolute;
  
}

body {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items:center;
  justify-content: center;
}

.text{
  font-size: 3em;
  /*animation: showfade 4s linear 2s both;*/
  animation-name: showfade;
  animation-duration: 4s;
  animation-delay: .4s;
  animation-fill-mode: both;
  
}

.rect {
  
  background-color: #07f;
  width: 200px;
  height: 200px;
  /*animation: rect 2s  linear 6s both;*/
  animation-name: rect;
  animation-duration: 2s;
  animation-delay: 4.4s;
  animation-fill-mode: both;
}


@keyframes showfade {
  0%, 100% {
    opacity: 0;
    
  }
  
  50% {
    opacity: 100;
  }
}

@keyframes rect {
  from {
    transform: scale(0);
    border-radius: 100%;
  }
}
<div class="text">Hello</div>
<div class="rect"></div>

1
投票

如果我理解正确的话,你可以这样做:

body {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0;
  height: 100vh;
}

.step3 {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 250px;
  width: 250px;
  border-radius: 50%;
  animation: stretch 10s 3s ease-out forwards;
}

.step3 > span {
  opacity: 0;
  animation: fadeInOut 3s;
}

@keyframes stretch {
  0%, 100% {transform: scale(.3); background: blue}
  100% {
    transform: scale(1.5);
    border-radius: 0;
  }
}

@keyframes fadeInOut {
  50% {opacity: 1}
  100% {opacity: 0}
}
<div class="step3"><span>Hello</span></div>
© www.soinside.com 2019 - 2024. All rights reserved.