【发布时间】: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