你不能用CSS Animations来做,但如果你不介意将它转换成 JavaScript,那么你可以使用Web Animations API来代替。
相当于这样的 CSS 动画:
.css-animation-ease {
animation-name: move-animation;
animation-duration: 3s;
}
@keyframes move-animation {
25% {
transform: translate(100px, 0);
}
50% {
transform: translate(100px, 100px);
background-color: green;
}
75% {
transform: translate(0, 100px);
}
100% {
transform: translate(0, 200px);
background-color: blue;
}
}
将是这个 JS 代码:
box.animate(
[
{ easing: "ease" },
{ transform: "translate(100px, 0)", offset: 0.25, easing: "ease" },
{ transform: "translate(100px, 100px)", backgroundColor: "green",
offset: 0.5, easing: "ease" },
{ transform: "translate(0, 100px)", offset: 0.75, easing: "ease" },
{ transform: "translate(0, 200px)", backgroundColor: "blue", easing: "ease" },
],
{ duration: 3000 }
);
但是,您不需要传递offset
和easing
参数。如果省略它们,则关键帧将均匀分布,您可以使用easing
将应用于整个动画的选项定义计时功能。
box.animate(
[
{},
{ transform: "translate(100px, 0)" },
{ transform: "translate(100px, 100px)", backgroundColor: "green" },
{ transform: "translate(0, 100px)" },
{ transform: "translate(0, 200px)", backgroundColor: "blue" },
],
{ duration: 3000, easing: "ease" }
);
这是比较这两种方法的示例(CSS Animation vs Web Animations API)
document.getElementById("animateAll").addEventListener("click", () => {
for (const btn of document.querySelectorAll(".examples button")) {
btn.click();
}
});
document
.getElementById("cssAnimationLinearBtn")
.addEventListener("click", () => {
const box = document.getElementById("cssAnimationLinear");
box.classList.add("css-animation-linear");
box.addEventListener("animationend", () => {
box.classList.remove("css-animation-linear");
});
});
document.getElementById("cssAnimationEaseBtn").addEventListener("click", () => {
const box = document.getElementById("cssAnimationEase");
box.classList.add("css-animation-ease");
box.addEventListener("animationend", () => {
box.classList.remove("css-animation-ease");
});
});
document
.getElementById("webAnimationLinearBtn")
.addEventListener("click", () => {
const box = document.getElementById("webAnimationLinear");
box.animate(
[
{},
{ transform: "translate(100px, 0)" },
{ transform: "translate(100px, 100px)", backgroundColor: "green" },
{ transform: "translate(0, 100px)" },
{ transform: "translate(0, 200px)", backgroundColor: "blue" },
],
{ duration: 3000 }
);
});
document
.getElementById("webAnimationEaseOffsetBtn")
.addEventListener("click", () => {
const box = document.getElementById("webAnimationEaseOffset");
box.animate(
[
{ easing: "ease" },
{ transform: "translate(100px, 0)", offset: 0.25, easing: "ease" },
{
transform: "translate(100px, 100px)",
backgroundColor: "green",
offset: 0.5,
easing: "ease",
},
{ transform: "translate(0, 100px)", offset: 0.75, easing: "ease" },
{
transform: "translate(0, 200px)",
backgroundColor: "blue",
easing: "ease",
},
],
{ duration: 3000 }
);
});
document.getElementById("webAnimationEaseBtn").addEventListener("click", () => {
const box = document.getElementById("webAnimationEase");
box.animate(
[
{},
{ transform: "translate(100px, 0)" },
{ transform: "translate(100px, 100px)", backgroundColor: "green" },
{ transform: "translate(0, 100px)" },
{ transform: "translate(0, 200px)", backgroundColor: "blue" },
],
{ duration: 3000, easing: "ease" }
);
});
.examples {
display: grid;
grid-template-columns: repeat(5, 130px);
}
.box {
width: 30px;
height: 30px;
background-color: red;
}
.box.css-animation-linear {
animation: move-animation 3s linear 1;
}
.box.css-animation-ease {
animation-name: move-animation;
animation-duration: 3s;
}
@keyframes move-animation {
25% {
transform: translate(100px, 0);
}
50% {
transform: translate(100px, 100px);
background-color: green;
}
75% {
transform: translate(0, 100px);
}
100% {
transform: translate(0, 200px);
background-color: blue;
}
}
<button id="animateAll">Animate all</button>
<div class="examples">
<div>
<span style="color:brown">CSS Animation API (linear)</span>
<button id="cssAnimationLinearBtn">Animate</button>
<div id="cssAnimationLinear" class="box"></div>
</div>
<div>
<span style="color:green">CSS Animation API (ease)</span>
<button id="cssAnimationEaseBtn">Animate</button>
<div id="cssAnimationEase" class="box"></div>
</div>
<div>
<span style="color:brown">Web Animation API (linear)</span>
<button id="webAnimationLinearBtn">Animate</button>
<div id="webAnimationLinear" class="box"></div>
</div>
<div>
<span style="color:green">Web Animation API (ease, offset)</span>
<button id="webAnimationEaseOffsetBtn">Animate</button>
<div id="webAnimationEaseOffset" class="box"></div>
</div>
<div>
<strong>Web Animation API (ease)</strong>
<button id="webAnimationEaseBtn">Animate</button>
<div id="webAnimationEase" class="box"></div>
</div>
</div>