【问题标题】:Use javascript in order to set the current percentage of a css animation使用 javascript 来设置 css 动画的当前百分比
【发布时间】:2015-06-13 08:25:11
【问题描述】:

我使用 css 动画来表示一天的太阳周期(从早上 6 点到下午 6 点)。 不幸的是,我还希望能够根据一天中的时间设置当前百分比(例如,如果当前时间是上午 12 点,我想将太阳放在动画的 50% 处。

这是我的 jsfiddle : http://jsfiddle.net/vyhjt6mu/3/

这是我的代码:

/* Chrome, Safari, Opera */

@-webkit-keyframes sunAnimation {
  0% {
    left: -100px;
    top: 30%;
  }
  25% {
    left: calc(25% - 25px);
    top: 20%;
  }
  50% {
    left: calc(50% - 40px);
    top: 10%;
  }
  75% {
    left: calc(75% + 25px);
    top: 20%;
  }
  100% {
    left: calc(100% + 100px);
    top: 30%;
  }
}
@keyframes sunAnimation {
  0% {
    left: -100px;
    top: 30%;
  }
  25% {
    left: calc(25% - 25px);
    top: 20%;
  }
  50% {
    left: calc(50% - 40px);
    top: 10%;
  }
  75% {
    left: calc(75% + 25px);
    top: 20%;
  }
  100% {
    left: calc(100% + 100px);
    top: 30%;
  }
}
.sun {
  width: 100px;
  height: 100px;
  position: absolute;
  animation-name: sunAnimation;
  animation-duration: 60s;
  animation-timing-function: linear;
  -webkit-animation-name: sunAnimation;
  /* Chrome, Safari, Opera */
  -webkit-animation-duration: 60s;
  /* Chrome, Safari, Opera */
  -webkit-animation-timing-function: linear;
  /* Chrome, Safari, Opera */
}

如何简单地设置 css3 动画的当前百分比?如果这样做非常复杂,是否存在为此的库?

感谢您的帮助。

不是重复因为需求不同。

【问题讨论】:

  • 不是一个完整的答案,但您可能会发现这很有帮助:jsfiddle.net/jbutler483/tgbcd9f2。它将图像移动一定百分比,具体取决于小时。
  • 如果我可以肯定地批评一下,这个动画也可以写成 CSS 更擅长的变换它会更平滑(你可以看到它现在正在采取的步骤)并且少很多资源流失。 greensock.com/css-performance
  • OddDev :需求不同。不是重复的。 jbutler483:谢谢,但在我的情况下可能会很困难,因为我有 4 个不同的步骤。 Shikkediel:有趣,我会看到的,谢谢。

标签: javascript css animation


【解决方案1】:

您可以指定一个否定的animation-delay 属性。

示例:http://jsfiddle.net/vyhjt6mu/4/

在那个例子中,我设置了animation-delay: -30s,所以动画将从中间点开始


对于这项任务,您可以像这样在 CSS 中设置 24 不同的类(每小时一个)

.h00 {
  animation-delay: 0s; 
  -webkit-animation-delay: 0s; 
}

.h01 {
  animation-delay: -2.5s; 
  -webkit-animation-delay: -2.5s; 
}

.h02 {
  animation-delay: -5s; 
  -webkit-animation-delay: -5s; 
}

...

.h22 {
  animation-delay: -55s; 
  -webkit-animation-delay: -55s; 
}

.h23 {
  animation-delay: -57.5s; 
  -webkit-animation-delay: -57.5s; 
}

每个小时之间的延迟差是2.5秒(60s/24);然后,通过 JS,通过getHours() 方法获取当前时间并将正确的类名应用于您的元素

【讨论】:

  • 我喜欢您的解决方案,简单且实施起来不(那么)无聊。谢谢。
【解决方案2】:

要回答您的实际问题: 是的,您可以为此使用 CSS-vars 和 JS 的组合。

$(function () {

	$(window).mousemove(function (e) {
  	var t =  (-1/$(window).width()*e.pageX);
  	document.documentElement.style.setProperty('--delay', t+'s');
  });
});
:root {
    --delay: 0;
}

.box {
  width: 20px;
  height: 20px;
  background-color: #ff6600;
  animation: move 1s;
  animation-play-state: paused;
  animation-delay: var(--delay);
}

@keyframes move {
  100% { 
    transform: translate(200px, 100px) scale(2) rotate(180deg);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box">
</div>

【讨论】:

  • 这是一个很好的答案。出于某种原因,当我将您的解决方案应用于我的代码时,只要我将延迟设置为 > 0,动画总是会跳转到 100%。我通过负延迟和反转动画来缓解这种情况。奇迹般有效。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多