扩展已翻译元素的第一个动画的最终状态
Extend the final state of the first animation for translated element
我知道这个问题在这里被问了很多次,但我要求对这类动画采用不同的方法。
问题:
第二个盒子有多个动画,试图创建与第一个相同的效果。似乎转换 属性 正在被覆盖 (应该如此)*。我正在尝试使用第二个动画的属性扩展第一个动画(第二个框)。尝试使用 animation-fill-mode: forwards
但没有成功。也许我缺少一些基本的东西。香草 CSS 可以吗?
The ‘animation-name’ property defines a list of animations that apply.
If multiple animations are
attempting to modify the same property, then the animation closest to
the end of the list of names wins.
要求:
分开 move2-right
和 move2-down
关键帧规则,但在同一元素上工作,保留第一个动画转换。如果有这种动画的替代方法,请指导我。
当前输出:
.animation-1,
.animation-2 {
width: 200px;
height: 200px;
display: inline-block;
background: white;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
}
.movable-1,
.movable-2 {
background: #41A186;
width: 50px;
height: 50px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
text-align: center;
vertical-align: middle;
line-height: 50px;
}
.movable-1 {
-webkit-animation-name: move1;
animation-name: move1;
-webkit-animation-duration: 4s;
animation-duration: 4s;
-webkit-animation-delay: 0s;
animation-delay: 0s;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
}
.movable-2 {
-webkit-animation-name: move2-right, move2-down;
animation-name: move2-right, move2-down;
-webkit-animation-duration: 2s, 2s;
animation-duration: 2s, 2s;
-webkit-animation-delay: 4s, 6s;
animation-delay: 4s, 6s;
-webkit-animation-fill-mode: forwards, forwards;
animation-fill-mode: forwards, forwards;
}
@-webkit-keyframes move1 {
0% {
-webkit-transform: translateX(0px);
transform: translateX(0px);
}
50% {
-webkit-transform: translateX(30px);
transform: translateX(30px);
}
100% {
-webkit-transform: translateX(30px) translateY(50px);
transform: translateX(30px) translateY(50px);
}
}
@keyframes move1 {
0% {
-webkit-transform: translateX(0px);
transform: translateX(0px);
}
50% {
-webkit-transform: translateX(30px);
transform: translateX(30px);
}
100% {
-webkit-transform: translateX(30px) translateY(50px);
transform: translateX(30px) translateY(50px);
}
}
@-webkit-keyframes move2-right {
0% {
-webkit-transform: translateX(0px);
transform: translateX(0px);
}
50% {
-webkit-transform: translateX(30px);
transform: translateX(30px);
}
100% {
-webkit-transform: translateX(30px);
transform: translateX(30px);
}
}
@keyframes move2-right {
0% {
-webkit-transform: translateX(0px);
transform: translateX(0px);
}
50% {
-webkit-transform: translateX(30px);
transform: translateX(30px);
}
100% {
-webkit-transform: translateX(30px);
transform: translateX(30px);
}
}
@-webkit-keyframes move2-down {
0% {
-webkit-transform: translateY(0px);
transform: translateY(0px);
}
50% {
-webkit-transform: translateY(50px);
transform: translateY(50px);
}
100% {
-webkit-transform: translateY(50px);
transform: translateY(50px);
}
}
@keyframes move2-down {
0% {
-webkit-transform: translateY(0px);
transform: translateY(0px);
}
50% {
-webkit-transform: translateY(50px);
transform: translateY(50px);
}
100% {
-webkit-transform: translateY(50px);
transform: translateY(50px);
}
}
<div class="animation-1">
<div class="movable-1">1</div>
</div>
<div class="animation-2">
<div class="movable-2">2</div>
</div>
这里有一个 fiddle 你可以玩:JSfiddle
据我所知,这对于纯 CSS 是不可能的,因为(正如您已经指出的那样),当我们向已经具有转换的元素添加额外的转换规则时,整个转换被重置,因为它覆盖并且不附加到现有转换。
使用 JS 可能可以实现,但即使那样也很难,因为我们必须执行以下操作:
- 在第一个动画完成时处理
animationend
事件。
- 在处理程序中,获取
translateX(...)
像素。
- 获取下一个动画的CSS关键帧规则,修改它们以
translateX(...)
作为变换堆栈的第一部分。
注意:我假设你遇到的情况是完全没有办法使用问题中提到的第一种方法
实现类似效果的另一种方法是为元素的 margin
或 position
设置动画,而不是使用 transform: translate()
。这种方法的一个主要缺点是这不会在 GPU 层完成(不像 transform
),因此当多个这样的动画同时发生时会更慢(并且可能也很昂贵)。
使用边距:
下面的代码片段通过动画 margin-left
和 margin-top
属性来实现效果。
.animation-1,
.animation-2,
.animation-3 {
width: 200px;
height: 200px;
display: inline-block;
background: white;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
vertical-align: middle;
}
.movable-1,
.movable-2,
.movable-3 {
background: #41A186;
width: 50px;
height: 50px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
text-align: center;
vertical-align: middle;
line-height: 50px;
}
.movable-1 {
animation-name: move1;
animation-duration: 4s;
animation-delay: 0s;
animation-fill-mode: forwards;
}
.movable-2 {
animation-name: move2-right, move2-down;
animation-duration: 2s, 2s;
animation-delay: 4s, 6s;
animation-fill-mode: forwards, forwards;
animation-timing-function: linear;
}
.movable-3 {
animation-name: move3-diagonal;
animation-duration: 4s;
animation-delay: 8s;
animation-fill-mode: forwards;
animation-timing-function: linear;
}
@keyframes move1 {
0% {
transform: translateX(0px);
}
50% {
transform: translateX(30px);
}
100% {
transform: translateX(30px) translateY(50px);
}
}
@keyframes move2-right {
0% {
margin-left: 0px;
}
100% {
margin-left: 30px;
}
}
@keyframes move2-down {
0% {
margin-top: 0px;
}
100% {
margin-top: 50px;
}
}
@keyframes move3-diagonal
{
0% {
margin-top: 0px;
margin-left: 0px;
}
100% {
margin-top: 50px;
margin-left: 30px;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="animation-1">
<div class="movable-1">1</div>
</div>
<div class="animation-2">
<div class="movable-2">2</div>
</div>
<div class="animation-3">
<div class="movable-3">3</div>
</div>
使用位置:
此代码段通过动画 left
和 top
属性实现了相同的效果。子元素有position: absolute
.
.animation-1,
.animation-2,
.animation-3 {
width: 200px;
height: 200px;
display: inline-block;
background: white;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
position: relative;
}
.movable-1,
.movable-2,
.movable-3 {
background: #41A186;
width: 50px;
height: 50px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
text-align: center;
vertical-align: middle;
line-height: 50px;
position: absolute;
}
.movable-1 {
animation-name: move1;
animation-duration: 4s;
animation-delay: 0s;
animation-fill-mode: forwards;
}
.movable-2 {
animation-name: move2-right, move2-down;
animation-duration: 2s, 2s;
animation-delay: 4s, 6s;
animation-fill-mode: forwards, forwards;
animation-timing-function: linear;
}
.movable-3 {
animation-name: move3-diagonal;
animation-duration: 4s;
animation-delay: 8s;
animation-fill-mode: forwards;
animation-timing-function: linear;
}
@keyframes move1 {
0% {
transform: translateX(0px);
}
50% {
transform: translateX(30px);
}
100% {
transform: translateX(30px) translateY(50px);
}
}
@keyframes move2-right {
0% {
left: 0px;
}
100% {
left: 30px;
}
}
@keyframes move2-down {
0% {
top: 0px;
}
100% {
top: 50px;
}
}
@keyframes move3-diagonal {
0% {
top: 0px;
left: 0px;
}
100% {
top: 50px;
left: 30px;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="animation-1">
<div class="movable-1">1</div>
</div>
<div class="animation-2">
<div class="movable-2">2</div>
</div>
<div class="animation-3">
<div class="movable-3">3</div>
</div>
注意:正如我的回答here中所述,您当然可以添加一个包装元素并为其设置向下移动动画。这会产生与第一个相同的效果,但我不建议采用这种方法,因为它违背了你的问题的重点(我认为这是 - 如何将多个动画添加到同一元素并制作第二个从第一个结束的地方开始).
.animation-1,
.animation-2 {
width: 200px;
height: 200px;
display: inline-block;
background: white;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
}
.movable-1,
.movable-2 {
background: #41A186;
width: 50px;
height: 50px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
text-align: center;
vertical-align: middle;
line-height: 50px;
}
.movable-1 {
animation-name: move1;
animation-duration: 4s;
animation-delay: 0s;
animation-fill-mode: forwards;
}
.movable-2 {
animation-name: move2-right;
animation-duration: 2s;
animation-delay: 4s;
animation-fill-mode: forwards;
}
.movable-2-wrapper {
animation-name: move2-down;
animation-duration: 2s;
animation-delay: 6s;
animation-fill-mode: forwards;
}
@keyframes move1 {
0% {
transform: translateX(0px);
}
50% {
transform: translateX(30px);
}
100% {
transform: translateX(30px) translateY(50px);
}
}
@keyframes move2-right {
0% {
transform: translateX(0px);
}
50% {
transform: translateX(30px);
}
100% {
transform: translateX(30px);
}
}
@keyframes move2-down {
0% {
transform: translateY(0px);
}
50% {
transform: translateY(50px);
}
100% {
transform: translateY(50px);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="animation-1">
<div class="movable-1">1</div>
</div>
<div class="animation-2">
<div class='movable-2-wrapper'>
<div class="movable-2">2</div>
</div>
</div>
我知道这个问题在这里被问了很多次,但我要求对这类动画采用不同的方法。
问题:
第二个盒子有多个动画,试图创建与第一个相同的效果。似乎转换 属性 正在被覆盖 (应该如此)*。我正在尝试使用第二个动画的属性扩展第一个动画(第二个框)。尝试使用 animation-fill-mode: forwards
但没有成功。也许我缺少一些基本的东西。香草 CSS 可以吗?
The ‘animation-name’ property defines a list of animations that apply. If multiple animations are attempting to modify the same property, then the animation closest to the end of the list of names wins.
要求:
分开 move2-right
和 move2-down
关键帧规则,但在同一元素上工作,保留第一个动画转换。如果有这种动画的替代方法,请指导我。
当前输出:
.animation-1,
.animation-2 {
width: 200px;
height: 200px;
display: inline-block;
background: white;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
}
.movable-1,
.movable-2 {
background: #41A186;
width: 50px;
height: 50px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
text-align: center;
vertical-align: middle;
line-height: 50px;
}
.movable-1 {
-webkit-animation-name: move1;
animation-name: move1;
-webkit-animation-duration: 4s;
animation-duration: 4s;
-webkit-animation-delay: 0s;
animation-delay: 0s;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
}
.movable-2 {
-webkit-animation-name: move2-right, move2-down;
animation-name: move2-right, move2-down;
-webkit-animation-duration: 2s, 2s;
animation-duration: 2s, 2s;
-webkit-animation-delay: 4s, 6s;
animation-delay: 4s, 6s;
-webkit-animation-fill-mode: forwards, forwards;
animation-fill-mode: forwards, forwards;
}
@-webkit-keyframes move1 {
0% {
-webkit-transform: translateX(0px);
transform: translateX(0px);
}
50% {
-webkit-transform: translateX(30px);
transform: translateX(30px);
}
100% {
-webkit-transform: translateX(30px) translateY(50px);
transform: translateX(30px) translateY(50px);
}
}
@keyframes move1 {
0% {
-webkit-transform: translateX(0px);
transform: translateX(0px);
}
50% {
-webkit-transform: translateX(30px);
transform: translateX(30px);
}
100% {
-webkit-transform: translateX(30px) translateY(50px);
transform: translateX(30px) translateY(50px);
}
}
@-webkit-keyframes move2-right {
0% {
-webkit-transform: translateX(0px);
transform: translateX(0px);
}
50% {
-webkit-transform: translateX(30px);
transform: translateX(30px);
}
100% {
-webkit-transform: translateX(30px);
transform: translateX(30px);
}
}
@keyframes move2-right {
0% {
-webkit-transform: translateX(0px);
transform: translateX(0px);
}
50% {
-webkit-transform: translateX(30px);
transform: translateX(30px);
}
100% {
-webkit-transform: translateX(30px);
transform: translateX(30px);
}
}
@-webkit-keyframes move2-down {
0% {
-webkit-transform: translateY(0px);
transform: translateY(0px);
}
50% {
-webkit-transform: translateY(50px);
transform: translateY(50px);
}
100% {
-webkit-transform: translateY(50px);
transform: translateY(50px);
}
}
@keyframes move2-down {
0% {
-webkit-transform: translateY(0px);
transform: translateY(0px);
}
50% {
-webkit-transform: translateY(50px);
transform: translateY(50px);
}
100% {
-webkit-transform: translateY(50px);
transform: translateY(50px);
}
}
<div class="animation-1">
<div class="movable-1">1</div>
</div>
<div class="animation-2">
<div class="movable-2">2</div>
</div>
这里有一个 fiddle 你可以玩:JSfiddle
据我所知,这对于纯 CSS 是不可能的,因为(正如您已经指出的那样),当我们向已经具有转换的元素添加额外的转换规则时,整个转换被重置,因为它覆盖并且不附加到现有转换。
使用 JS 可能可以实现,但即使那样也很难,因为我们必须执行以下操作:
- 在第一个动画完成时处理
animationend
事件。 - 在处理程序中,获取
translateX(...)
像素。 - 获取下一个动画的CSS关键帧规则,修改它们以
translateX(...)
作为变换堆栈的第一部分。
注意:我假设你遇到的情况是完全没有办法使用问题中提到的第一种方法
实现类似效果的另一种方法是为元素的 margin
或 position
设置动画,而不是使用 transform: translate()
。这种方法的一个主要缺点是这不会在 GPU 层完成(不像 transform
),因此当多个这样的动画同时发生时会更慢(并且可能也很昂贵)。
使用边距:
下面的代码片段通过动画 margin-left
和 margin-top
属性来实现效果。
.animation-1,
.animation-2,
.animation-3 {
width: 200px;
height: 200px;
display: inline-block;
background: white;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
vertical-align: middle;
}
.movable-1,
.movable-2,
.movable-3 {
background: #41A186;
width: 50px;
height: 50px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
text-align: center;
vertical-align: middle;
line-height: 50px;
}
.movable-1 {
animation-name: move1;
animation-duration: 4s;
animation-delay: 0s;
animation-fill-mode: forwards;
}
.movable-2 {
animation-name: move2-right, move2-down;
animation-duration: 2s, 2s;
animation-delay: 4s, 6s;
animation-fill-mode: forwards, forwards;
animation-timing-function: linear;
}
.movable-3 {
animation-name: move3-diagonal;
animation-duration: 4s;
animation-delay: 8s;
animation-fill-mode: forwards;
animation-timing-function: linear;
}
@keyframes move1 {
0% {
transform: translateX(0px);
}
50% {
transform: translateX(30px);
}
100% {
transform: translateX(30px) translateY(50px);
}
}
@keyframes move2-right {
0% {
margin-left: 0px;
}
100% {
margin-left: 30px;
}
}
@keyframes move2-down {
0% {
margin-top: 0px;
}
100% {
margin-top: 50px;
}
}
@keyframes move3-diagonal
{
0% {
margin-top: 0px;
margin-left: 0px;
}
100% {
margin-top: 50px;
margin-left: 30px;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="animation-1">
<div class="movable-1">1</div>
</div>
<div class="animation-2">
<div class="movable-2">2</div>
</div>
<div class="animation-3">
<div class="movable-3">3</div>
</div>
使用位置:
此代码段通过动画 left
和 top
属性实现了相同的效果。子元素有position: absolute
.
.animation-1,
.animation-2,
.animation-3 {
width: 200px;
height: 200px;
display: inline-block;
background: white;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
position: relative;
}
.movable-1,
.movable-2,
.movable-3 {
background: #41A186;
width: 50px;
height: 50px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
text-align: center;
vertical-align: middle;
line-height: 50px;
position: absolute;
}
.movable-1 {
animation-name: move1;
animation-duration: 4s;
animation-delay: 0s;
animation-fill-mode: forwards;
}
.movable-2 {
animation-name: move2-right, move2-down;
animation-duration: 2s, 2s;
animation-delay: 4s, 6s;
animation-fill-mode: forwards, forwards;
animation-timing-function: linear;
}
.movable-3 {
animation-name: move3-diagonal;
animation-duration: 4s;
animation-delay: 8s;
animation-fill-mode: forwards;
animation-timing-function: linear;
}
@keyframes move1 {
0% {
transform: translateX(0px);
}
50% {
transform: translateX(30px);
}
100% {
transform: translateX(30px) translateY(50px);
}
}
@keyframes move2-right {
0% {
left: 0px;
}
100% {
left: 30px;
}
}
@keyframes move2-down {
0% {
top: 0px;
}
100% {
top: 50px;
}
}
@keyframes move3-diagonal {
0% {
top: 0px;
left: 0px;
}
100% {
top: 50px;
left: 30px;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="animation-1">
<div class="movable-1">1</div>
</div>
<div class="animation-2">
<div class="movable-2">2</div>
</div>
<div class="animation-3">
<div class="movable-3">3</div>
</div>
注意:正如我的回答here中所述,您当然可以添加一个包装元素并为其设置向下移动动画。这会产生与第一个相同的效果,但我不建议采用这种方法,因为它违背了你的问题的重点(我认为这是 - 如何将多个动画添加到同一元素并制作第二个从第一个结束的地方开始).
.animation-1,
.animation-2 {
width: 200px;
height: 200px;
display: inline-block;
background: white;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
}
.movable-1,
.movable-2 {
background: #41A186;
width: 50px;
height: 50px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 1px 5px 0 rgba(0, 0, 0, 0.12);
text-align: center;
vertical-align: middle;
line-height: 50px;
}
.movable-1 {
animation-name: move1;
animation-duration: 4s;
animation-delay: 0s;
animation-fill-mode: forwards;
}
.movable-2 {
animation-name: move2-right;
animation-duration: 2s;
animation-delay: 4s;
animation-fill-mode: forwards;
}
.movable-2-wrapper {
animation-name: move2-down;
animation-duration: 2s;
animation-delay: 6s;
animation-fill-mode: forwards;
}
@keyframes move1 {
0% {
transform: translateX(0px);
}
50% {
transform: translateX(30px);
}
100% {
transform: translateX(30px) translateY(50px);
}
}
@keyframes move2-right {
0% {
transform: translateX(0px);
}
50% {
transform: translateX(30px);
}
100% {
transform: translateX(30px);
}
}
@keyframes move2-down {
0% {
transform: translateY(0px);
}
50% {
transform: translateY(50px);
}
100% {
transform: translateY(50px);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="animation-1">
<div class="movable-1">1</div>
</div>
<div class="animation-2">
<div class='movable-2-wrapper'>
<div class="movable-2">2</div>
</div>
</div>