Rou*_*ica 7 html javascript css
我@keyframes在 2016 年 1 月学习了 CSS3语法,两年多后,我发现自己@keyframes在很多工作中都使用了动画(比 CSS3 过渡更复杂,比基于 javascript 的动画更简单)。
我真正想念的一件事是能够以@keyframes秒而不是百分比来表达。是否有任何技巧可以实现这一目标?
我知道我可以使用以下100shack 来循环显示彩虹色,每 3 秒循环一次:
div {
width: 120px;
height: 120px;
background-color: violet;
animation: myAnimation 100s;
}
@keyframes myAnimation {
0% {background-color: red;}
3% {background-color: orange;}
6% {background-color: yellow;}
9% {background-color: green;}
12% {background-color: cyan;}
15% {background-color: blue;}
18% {background-color: violet;}
100% {background-color: violet;}
}Run Code Online (Sandbox Code Playgroud)
<div></div>Run Code Online (Sandbox Code Playgroud)
但这意味着动画在(有效)完成后仍在运行(尽管在不知不觉中)另外 82 秒。除其他问题外,这使多次迭代变得遥不可及。
我真正想写的很简单:
@keyframes myAnimation {
0s {background-color: red;}
3s {background-color: orange;}
6s {background-color: yellow;}
9s {background-color: green;}
12s {background-color: cyan;}
15s {background-color: blue;}
18s {background-color: violet;}
}
Run Code Online (Sandbox Code Playgroud)
有没有比我在上面的代码框中详述的方法更好的方法?
事后我意识到我可能把上面的例子做得太简单了,因为它涉及对单个元素进行动画处理,而我的问题最初是因为希望对多个元素进行同步动画处理。
所以,这是一个稍微复杂一点的例子,展示了一个更接近于首先引起我的问题的设置:
@keyframes myAnimation {
0s {background-color: red;}
3s {background-color: orange;}
6s {background-color: yellow;}
9s {background-color: green;}
12s {background-color: cyan;}
15s {background-color: blue;}
18s {background-color: violet;}
}
Run Code Online (Sandbox Code Playgroud)
div {
display: inline-block;
width: 48px;
height: 48px;
margin-right: 6px;
}
div:nth-of-type(1) {
background-color: red;
}
div:nth-of-type(2) {
background-color: orange;
animation: myAnimationOrange 100s;
}
div:nth-of-type(3) {
background-color: yellow;
animation: myAnimationYellow 100s;
}
div:nth-of-type(4) {
background-color: green;
animation: myAnimationGreen 100s;
}
div:nth-of-type(5) {
background-color: cyan;
animation: myAnimationCyan 100s;
}
div:nth-of-type(6) {
background-color: violet;
animation: myAnimationViolet 100s;
}
@keyframes myAnimationOrange {
0% {background-color: white;}
1% {background-color: white;}
2% {background-color: orange;}
100% {background-color: orange;}
}
@keyframes myAnimationYellow {
0% {background-color: white;}
2% {background-color: white;}
3% {background-color: yellow;}
100% {background-color: yellow;}
}
@keyframes myAnimationGreen {
0% {background-color: white;}
3% {background-color: white;}
4% {background-color: green;}
100% {background-color: green;}
}
@keyframes myAnimationCyan {
0% {background-color: white;}
4% {background-color: white;}
5% {background-color: cyan;}
100% {background-color: cyan;}
}
@keyframes myAnimationViolet {
0% {background-color: white;}
5% {background-color: white;}
6% {background-color: violet;}
100% {background-color: violet;}
}Run Code Online (Sandbox Code Playgroud)
Kai*_*ido 14
不要忘了,你可以在同一个元素上运行多个动画,并且可以设置自己的duration,delay和所有其他animation-...独立的规则。
例如,您可以将所有关键帧拆分为单键 @keyframes 规则。
然后很容易控制他们什么时候开始和链接他们。
div {
width: 120px;
height: 120px;
background-color: violet;
animation-fill-mode: forwards;
animation-name: orange, yellow, green, cyan, blue, violet;
animation-delay: 0s, 3s, 6s, 9s, 12s, 15s, 18s;
animation-duration: 3s; /* same for all */
}
@keyframes orange {
to { background-color: orange; }
}
@keyframes yellow {
to { background-color: yellow; }
}
@keyframes green {
to { background-color: green; }
}
@keyframes cyan {
to { background-color: cyan; }
}
@keyframes blue {
to { background-color: blue; }
}
@keyframes violet {
to { background-color: violet; }
}Run Code Online (Sandbox Code Playgroud)
<div></div>Run Code Online (Sandbox Code Playgroud)
在这种情况下,您甚至不需要在同一个元素上组合多个动画,而只需animation-delay相应地设置:
div {
/* same for all */
width: 60px;
height: 60px;
display: inline-block;
background-color: white;
animation-fill-mode: forwards;
animation-duration: 3s;
}
div:nth-of-type(1) {
animation-name: orange;
animation-delay: 0s;
}
div:nth-of-type(2) {
animation-name: yellow;
animation-delay: 3s;
}
div:nth-of-type(3) {
animation-name: green;
animation-delay: 6s;
}
div:nth-of-type(4) {
animation-name: cyan;
animation-delay: 9s;
}
div:nth-of-type(5) {
animation-name: blue;
animation-delay: 12s;
}
div:nth-of-type(6) {
animation-name: violet;
animation-delay: 15s;
}
@keyframes orange {
to { background-color: orange; }
}
@keyframes yellow {
to { background-color: yellow; }
}
@keyframes green {
to { background-color: green; }
}
@keyframes cyan {
to { background-color: cyan; }
}
@keyframes blue {
to { background-color: blue; }
}
@keyframes violet {
to { background-color: violet; }
}Run Code Online (Sandbox Code Playgroud)
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>Run Code Online (Sandbox Code Playgroud)
但如果你想将两者结合在一起,也是有可能的:
div {
/* same for all */
width: 60px;
height: 60px;
display: inline-block;
background-color: white;
animation-fill-mode: forwards;
animation-duration: 3s;
}
div:nth-of-type(1) {
animation-name: orange, yellow, green, cyan, blue, violet;
animation-delay: 0s, 3s, 6s, 9s, 12s, 15s;
}
div:nth-of-type(2) {
animation-name: yellow, green, cyan, blue, violet;
animation-delay: 3s, 6s, 9s, 12s, 15s;
}
div:nth-of-type(3) {
animation-name: green, cyan, blue, violet;
animation-delay: 6s, 9s, 12s, 15s;
}
div:nth-of-type(4) {
animation-name: cyan, blue, violet;
animation-delay: 9s, 12s, 15s;
}
div:nth-of-type(5) {
animation-name: blue, violet;
animation-delay: 12s, 15s;
}
div:nth-of-type(6) {
animation-name: violet;
animation-delay: 15s;
}
@keyframes orange {
to { background-color: orange; }
}
@keyframes yellow {
to { background-color: yellow; }
}
@keyframes green {
to { background-color: green; }
}
@keyframes cyan {
to { background-color: cyan; }
}
@keyframes blue {
to { background-color: blue; }
}
@keyframes violet {
to { background-color: violet; }
}Run Code Online (Sandbox Code Playgroud)
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4655 次 |
| 最近记录: |