【问题标题】:Infinite fade lines with reverse direction反向的无限淡入淡出线
【发布时间】:2016-01-07 22:21:52
【问题描述】:

我有三个底部有边框的跨度,需要它从上到下淡入淡出并反向。 如果我有 2 次迭代计数但无限次无法正常工作,它工作正常。

.line {
  border-bottom: 0.2em solid #333;
  display: block;
  margin: 0 auto;
  margin-top: 0.3em;
  width: 1.5em;
}
.lines span[class='line']:nth-child(1) {
  animation: 1.5s ease-in-out 0.2s 2 alternate fade_line;
}
.lines span[class='line']:nth-child(2) {
  animation: 1s ease-in-out 0.7s 2 alternate fade_line;
}
.line {
  animation: 0.5s ease-in-out 1.2s 2 alternate fade_line;
}
@keyframes fade_line {
  0%, 50% {
    border-bottom: 0.2em solid #333;
  }
  50%,
  100% {
    border-bottom: 0.2em solid #ddd;
  }
}
<div class="lines">
  <span class="line"></span>
  <span class="line"></span>
  <span class="line"></span>
</div>

Fiddle Demo

【问题讨论】:

  • 我对如何解决这个问题有点迷茫,但问题的原因是animation-delay 的工作方式。延迟仅在第一次添加。所以动画重叠。您可能会发现 this thread 对找到解决方案有点帮助。
  • 我找不到修复你的动画的方法,但here 是一段代码,可以产生类似的东西。不知道对你有没有帮助。

标签: css css-animations


【解决方案1】:

动画的问题在于animation-delay 实际上只适用于动画的第一次迭代。下面解释了为什么它在只有 2 次迭代时有效,为什么在无限次迭代时无效。

为什么只有 2 次迭代时它会起作用?

  • 首先span 在0.2s 开始动画(在animation-delay 之后)。在0.2s 到1.7s 之间,边框颜色从#333 到#ddd(在0.95s 是50% 的1.5s 持续时间是准确的)和1.7s 和3.2s 它之间从#ddd 返回(反向)到#333(准确地说是2.45s)。
  • 第二个span 在0.7s 开始动画。在0.7s 到1.7s 之间,边框颜色从#333 到#ddd(准确地说是1.2s),在1.7s 到2.7s 之间,它从#ddd 回到(反向)到#333(准确地说是2.2s)。
  • 第三个span 在1.2s 开始动画。在1.2s 到1.7s 之间,边框颜色从#333 到#ddd(准确地说是在1.45s)和在1.7s 到2.2s 之间它从#ddd 回到(反向)到#333(准确地说是1.95s)。
Element         | Iteration 1       | Iteration 2
---------------------------------------------------
First Span      | 0.95s             | 2.45s
Second Span     | 1.2s              | 2.2s
Third Span      | 1.45s             | 1.95s

如您所见,只要有 2 次迭代,它的流程就很好。


为什么将迭代次数设置为无限时不起作用?

现在让我们看看第三次和后续迭代会发生什么:

  • 第一个span - 在3.2s 到4.7s 之间,边框颜色从#333 到#ddd(准确地说是在3.95s),在4.7s 和6.2s 之间返回(反向)从#ddd 到#333(准确地说是5.45s)。
  • 第二个span - 在2.7s 到3.7s 之间,边框颜色从#333 到#ddd(准确地说是3.2s)和在3.7s 到4.7s 之间返回(反向)从#ddd 到#333(准确地说是4.2s)。
  • 第三个span - 在2.2s 到2.7s 之间,边框颜色从#333 到#ddd(准确地说是2.45s)和在2.7s 到3.2s 之间返回(反向)从#ddd 到#333(准确地说是2.95s)。
Element         | Iteration 1       | Iteration 2       | Iteration 3       | Iteration 4
------------------------------------------------------------------------------------------
First Span      | 0.95s             | 2.45s             | 3.95s             | 5.45s
Second Span     | 1.2s              | 2.2s              | 3.2s              | 4.2s
Third Span      | 1.45s             | 1.95s             | 2.45s             | 2.95s

如您所见,由于动画持续时间和延迟的配置方式,动画开始重叠,因此流程完全混乱。


解决方案:

通常,在无限循环动画的每次迭代之间引入延迟的方法是修改关键帧,以便它们为每次迭代添加相等的延迟。我在my answer here 中对此进行了解释。不幸的是,由于反向动画,您的情况要复杂得多。我无法修改关键帧以符合您的期望,但我希望上述解释有助于您理解问题。

如果您乐于使用其他方法来实现相同的效果,那么您可以查看linear-gradient 背景图像来创建汉堡效果,然后为其添加动画。

.lines {
  margin: 0 auto;
  height: 30px;
  width: 30px;
  background-image: linear-gradient(#333, #333), linear-gradient(#333, #333), linear-gradient(#333, #333);
  background-size: 100% 5px;
  background-position: 0px 5px, 0px 15px, 0px 25px;
  background-repeat: no-repeat;
  animation: bars 1.7s infinite alternate ease-in-out;
}
@keyframes bars {
  0% {
    background-image: linear-gradient(#333, #333), linear-gradient(#333, #333), linear-gradient(#333, #333);
  }
  33% {
    background-image: linear-gradient(#ddd, #ddd), linear-gradient(#333, #333), linear-gradient(#333, #333);
  }
  66% {
    background-image: linear-gradient(#ddd, #ddd), linear-gradient(#ddd, #ddd), linear-gradient(#333, #333);
  }
  100% {
    background-image: linear-gradient(#ddd, #ddd), linear-gradient(#ddd, #ddd), linear-gradient(#ddd, #ddd);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="lines"></div>

以下是使用您的初始方法的解决方案(由vals 提供)。但是你不能重复使用关键帧!

.line {
  border-bottom: 0.2em solid #333;
  display: block;
  margin: 0 auto;
  margin-top: 0.3em;
  width: 1.5em;
}
.line {
  animation: 1.7s ease-in-out infinite alternate fade_line1;
}
.line:nth-child(2) {
  animation-name: fade_line2;
}
.line:nth-child(3) {
  animation-name: fade_line3;
}
@keyframes fade_line1 { /* use 25% instead of 30% if the splits need to be equal among all 3 */
  0%, 30% {
    border-bottom-color: #333;
  }
  30%, 100% {
    border-bottom-color: #ddd;
  }
}
@keyframes fade_line2 {
  0%, 50% {
    border-bottom-color: #333;
  }
  50%, 100% {
    border-bottom-color: #ddd;
  }
}
@keyframes fade_line3 { /* use 75% instead of 70% if the splits need to be equal among all 3 */
  0%, 70% {
    border-bottom-color: #333;
  }
  70%, 100% {
    border-bottom-color: #ddd;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="lines">
  <span class="line"></span>
  <span class="line"></span>
  <span class="line"></span>
</div>

【讨论】:

  • 太棒了!也感谢您的解决方案和解释。我试图在纸上画出每个关键帧以便更好地理解:)
【解决方案2】:

在使用了很多时间和精力之后,我想使用 svg 创建一个(简单的)替代答案。

经过一个小时的 javascript 编码,我将发布到目前为止的情况。 我对代码不满意,因为它很复杂并且使用了相当多的 JavaScript 代码来为颜色过渡设置动画。

var stop = document.getElementById("stop2");

var dir = false;
var amount = 1;
var pause = false;

startanim();

function fade() {
  if (amount >= 100) {
    if (amount == 150) {
      dir = true;
    }
    amount++;
  } else if (amount <= 0) {
    if (amount == -50) {
      dir = false;
    }
    amount--;
  }
  if (dir) {
    stop.setAttribute("offset", amount + "%");
    amount--;
  } else {
    stop.setAttribute("offset", amount + "%");
    amount++;
  }
}

var interval = "id";

function startanim() {
  interval = setInterval(function() {
    fade();
  }, 10);
}
.handle {
  mask: url(#myMask);
}
<svg id="burger" viewBox="0 0 50 50" width="100" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <linearGradient id="Gradient1" x1="0" x2="0" y1="0" y2="1">
      <stop id="stop1" stop-color="black" offset="0%" />
      <stop id="stop2" stop-color="#ddd" offset="1%" />
      <stop id="stop4" stop-color="#ddd" offset="100%" />
    </linearGradient>
    <mask id="myMask" maskUnits="userSpaceOnUse" x="0" y="0" width="50" height="50">
      <rect x="0" y="0" width="50" height="50" fill="url(#Gradient1)" />
    </mask>
  </defs>
  <rect class="handle" x="20" y="12" width="30" height="5" />
  <rect class="handle" x="20" y="25" width="30" height="5" />
  <rect class="handle" x="20" y="37" width="30" height="5" />
</svg>

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 1970-01-01
    • 2021-04-13
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    相关资源
    最近更新 更多