【问题标题】:How can I make my keyframe animation start over smoothly?如何让我的关键帧动画顺利重新开始?
【发布时间】:2019-04-20 03:01:29
【问题描述】:

我用关键帧制作的动画没有流畅的结尾,而且跳到开头的速度非常快。我不知道如何使这个过渡更顺畅。

当动画结束并跳回起点时,第一项并不完全可见。

body {
  font-family: 'Poppins', sans-serif;
}

ul {
  list-style: none;
}

#flip {
  height: 43px;
  margin-top: -20px;
  overflow: hidden;
}

#flip .content {
  display: flex;
  flex-direction: column;
  align-items: center;
}

#flip h3 {
  font-weight: 400;
  font-size: 0.5em;
  color: white;
  text-transform: uppercase;
  text-align: right;
  display: inline-block;
  padding: 5px;
  margin-bottom: 10px;
  animation: flip 4s infinite;
  animation-delay: 1s;
}

#flip .one {
  background: #CD1517;
}

#flip .two {
  background: #068587;
}

#flip .three {
  background: #F2B134;
}

#flip .four {
  background: #6B50BF;
}

#flip .five {
  background: #4FB99F;
}

@-webkit-keyframes flip {
  0% {
      transform: translateY(0px);
  }
  20% {
      transform: translateY(-43px);
  }
  40% {
      transform: translateY(-90px);
  }
  60% {
      transform: translateY(-135px);
  }
  80% {
      transform: translateY(-180px);
  }
  100% {
      transform: translateY(0px);
  }
}
<ul>
  <li id="flip">
   <div class="content">
	<h3 class="one">developer</h3>
	<h3 class="two">designer</h3>
	<h3 class="three">programmer</h3>
	<h3 class="four">carodej</h3>
	<h3 class="five">alluneed</h3>
   </div>
  </li>
</ul>

结束动画可以制作从头到尾平滑跳转的动画。

【问题讨论】:

    标签: html css css-animations


    【解决方案1】:

    要解决动画滚动到顶部后第一个项目半可见的问题,您应该对所有项目应用相同的高度并正确垂直居中 h3-tag。为了方便起见,我稍微调整了您的 html。

    要平滑动画,您需要更改关键帧。首先使用元素高度的倍数作为 translateY。然后你必须改变百分比。通过让过渡在动画持续时间的 10% 处开始,您的第一个项目将在动画重新启动后显示更长时间。

    body {
      font-family: 'Poppins', sans-serif;
    }
    
    ul {
      list-style: none;
    }
    
    #flip {
      height: 43px;
      overflow: hidden;
    }
    
    #flip .content {
      height: 43px; /* new */
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center; /* new */
    }
    
    #flip h3 {
      font-weight: 400;
      font-size: 0.5em;
      color: white;
      text-transform: uppercase;
      padding: 5px;
      animation: flip 6s infinite; /* changed */
      animation-delay: 1s;
    }
    
    #flip .one {
      background: #CD1517;
    }
    
    #flip .two {
      background: #068587;
    }
    
    #flip .three {
      background: #F2B134;
    }
    
    #flip .four {
      background: #6B50BF;
    }
    
    #flip .five {
      background: #4FB99F;
    }
    
    
    /* changed */
    @-webkit-keyframes flip {
      10%, 100% {
        transform: translateY(0px);
      }
      25% {
        transform: translateY(-43px);
      }
      40% {
        transform: translateY(-86px);
      }
      55% {
        transform: translateY(-129px);
      }
      75% {
        transform: translateY(-172px);
      }
    }
    <ul id="flip">
      <li class="content">
        <h3 class="one">developer</h3>
      </li>
      <li class="content">
        <h3 class="two">designer</h3>
      </li>
      <li class="content">
        <h3 class="three">programmer</h3>
      </li>
      <li class="content">
        <h3 class="four">carodej</h3>
      </li>
      <li class="content">
        <h3 class="five">alluneed</h3>
      </li>
    </ul>

    编辑:没有滚动到顶部效果的动画

    为了获得无缝的旋转效果,您需要进一步调整 translateY 值并添加更多关键帧。

    注意:提供的代码不是很流畅 - 尝试调整百分比和 translateY 值,直到动画适合您的需要。

    @-webkit-keyframes flip {
      0% {
        transform: translateY(15px);
      }
      10% {
        transform: translateY(0px);
      }
      30% {
        transform: translateY(-43px);
      }
      50% {
        transform: translateY(-86px);
      }
      70% {
        transform: translateY(-129px);
      }
      90% {
        transform: translateY(-172px);
      }
      100% {
        transform: translateY(-202px);
      }
    }
    

    【讨论】:

    • 如果我需要最后的动画而不是像滚动那样做动画,但像没人会看到的动画是 div 的结尾并在最后制作一些东西是什么让动画看起来像一个无限的动画没有动画的可见结尾?谢谢回答
    • @MatúšRebroš 查看我的编辑。它应该给你一个很好的起点。获得完美的动画是一项相当繁琐的任务,因为它取决于可用的空白、字体大小、填充/边距等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多