У меня есть 2 элемента: один прямоугольный, а другой - линия. Я перемещаю rect слева направо, как только это будет сделано, затем поверну line. Тогда я хочу, чтобы после поворота линии я хотел изменить цвет фона rect.
.rect {
position: absolute;
left: 0;
width: 100px;
height: 100px;
background: red;
animation: move 1s;
animation-fill-mode: forwards;
}
.line {
position: absolute;
top: 200px;
left: 100px;
height: 100px;
border-right: 2px solid green;
animation: rotate 1s;
animation-fill-mode: forwards;
animation-delay: 1.3s;
}
@-webkit-keyframes move {
to {
left: 200px;
}
}
@-webkit-keyframes rotate {
to {
transform: rotate(360deg);
}
}<div class = "rect"></div>
<div class = "line"></div>





вы можете несколько анимаций разделить запятыми. Просто добавьте задержку анимации ко второй анимации, которая меняет цвет
.rect {
position: absolute;
left: 0;
width: 100px;
height: 100px;
background: red;
animation: move 1s, colorChange 1s 2s;
animation-fill-mode: forwards;
}
.line {
position: absolute;
top: 200px;
left: 100px;
height: 100px;
border-right: 2px solid green;
animation: rotate 1s;
animation-fill-mode: forwards;
animation-delay: 1.3s;
}
@-webkit-keyframes move {
from {}
to {
left: 200px;
}
}
@-webkit-keyframes rotate {
from {}
to {
transform: rotate(360deg);
}
}
@keyframes colorChange {
to {
background-color: green;
}
}<div class = "rect"></div>
<div class = "line"></div>Как это?
В .rect добавьте вторую анимацию:
.rect {
position: absolute;
left: 0;
width: 100px;
height: 100px;
background: red;
animation: move 1s, turnGreen 2.3s;
animation-fill-mode: forwards;
}
Затем определите новую анимацию:
@-webkit-keyframes turnGreen{
0% {background: red;}
99% {background: red;}
100% {background: green;}
}
Я тестировал это на вашем JSFiddle, и, похоже, он работал так, как вы описали.
Вам не нужен дополнительный animation, вам просто нужно отрегулировать ключевой кадр% и изменить продолжительность на 2.3s, то есть 1s + 1.3s, если вы хотите, чтобы изменение цвет происходило одновременно в конце, если нет, то соответствующим образом отрегулируйте %. :
.rect {
position: absolute;
left: 0;
width: 100px;
height: 100px;
animation: move 2.3s forwards;
}
.line {
position: absolute;
top: 200px;
left: 100px;
height: 100px;
border-right: 2px solid green;
animation: rotate 1s 1.3s forwards;
}
@-webkit-keyframes move {
43.48%, 100% {left: 200px} /* 100% / 2.3 = 43.48% (around), which is 1s duration (like before), then keep it there till the end (100%) */
0%, 99.99% {background: red} /* keep it red 99.99% of the time */
100% {background: blue} /* but not 100% */
}
@-webkit-keyframes rotate {
to {transform: rotate(360deg)}
}<div class = "rect"></div>
<div class = "line"></div>