html - 如何让 css 动画在关键帧中更快地过渡

标签 html css animation

我创建了一个使用三个不同图像的 css 动画,然后在最后我让它返回到第一个图像。我的问题是每当图像转换时,它看起来都不自然。看起来好像旧图像淡出,新图像同时淡入。我希望收紧过渡,以便仍然有一点淡入淡出,但更快更自然。

我为图像设置了百分比,所以我什至不确定淡入淡出的来源。

有人知道我该怎么做吗?

#contact-success-animation {
	width: 100px;
	height: 100px;
	position: relative;
	margin: auto auto;
	-webkit-animation-name: success-animation; /* Chrome, Safari, Opera */
    -webkit-animation-duration: 15s; /* Chrome, Safari, Opera */
    -webkit-animation-iteration-count: 1; /* Chrome, Safari, Opera */
    -webkit-animation-direction: normal; /* Chrome, Safari, Opera */
	-webkit-animation-fill-mode: forwards; /* Chrome, Safari, Opera */
	animation-name: success-animation;
    animation-duration: 15s;
    animation-iteration-count: 1;
    animation-direction: normal;
	animation-fill-mode: forwards;
}

/* Chrome, Safari, Opera */
@-webkit-keyframes success-animation {
    0%   {background-image: url("http://optimumwebdesigns.com/images/smiley-face.png"); background-repeat: no-repeat;
background-size: 100% auto; height: 100px; width: 100px;}
    33%  {background-image: url("http://optimumwebdesigns.com/images/big-smile-face.png"); background-repeat: no-repeat;
background-size: 100% auto; height: 100px; width: 100px;}
    66%  {background-image: url("http://optimumwebdesigns.com/images/wink-smile-face.png"); background-repeat: no-repeat;
background-size: 100% auto; height: 100px; width: 100px;}
    100% {background-image: url("http://optimumwebdesigns.com/images/smiley-face.png"); background-repeat: no-repeat;
background-size: 100% auto; height: 100px; width: 100px;}
}

/* Standard syntax */
@keyframes success-animation {
    0%   {background-image: url("http://optimumwebdesigns.com/images/smiley-face.png"); background-repeat: no-repeat;
background-size: 100% auto; height: 100px; width: 100px;}
    33%  {background-image: url("http://optimumwebdesigns.com/images/big-smile-face.png"); background-repeat: no-repeat;
background-size: 100% auto; height: 100px; width: 100px;}
    66%  {background-image: url("http://optimumwebdesigns.com/images/wink-smile-face.png"); background-repeat: no-repeat;
background-size: 100% auto; height: 100px; width: 100px;}
    100% {background-image: url("http://optimumwebdesigns.com/images/smiley-face.png"); background-repeat: no-repeat;
background-size: 100% auto; height: 100px; width: 100px;}
}
<div id="contact-success-animation"></div>

最佳答案

使用 @keyframes,您可以通过编写 33%,65% 而不是 33% 来指定时间“区域”。这允许您屏蔽一个图像将出现在屏幕上的时间段,然后它会在一段时间后快速过渡到另一个图像,这将实现您正在寻找的内容。

这是一个示例,使用背景颜色代替图像 (jsFiddle):

HTML

<div class="Snappy">
  This is a snappy transition.
</div>
<div class="Smooth">
  This is a smooth transition.
</div>

CSS

.Smooth
{
  animation: smoothColors 10s infinite;
}

.Snappy
{
  animation: snappyColors 10s infinite;
}

/* Keyframes */

@keyframes smoothColors{
  0%{background: #ff0000;}
  33%{background: #00ff00;}
  66%{background: #0000ff;}
  100%{background: #ff0000;}
}

@keyframes snappyColors{
  0%,23%{background: #ff0000;}
  33%,56%{background: #00ff00;}
  66%, 90%{background: #0000ff;}
  100%{background: #ff0000;}
}

关于html - 如何让 css 动画在关键帧中更快地过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35367145/

相关文章:

javascript - 更改悬停框内文本的颜色

html - 如何更改 CSS 中的 URL?

javascript - 使用 jQuery 的淡入/淡出动画效果

c# - 如何修复 WinForms 窗体中的闪烁?

html - 如何使按钮看起来像标签?

html - 当 Flex 段落有多行或较长的单词而不是一行时,它的右边缘会占用更多空间

html - 使用列数时在悬停时显示文本

jquery - 像使用 jQuery 的选择下拉列表一样列表?

javascript - 如何使两个函数在单击时使用react

html - CSS - 内容中有空格但可以 href 的标签