我知道我们可以通过animation-fill-mode: forwards;
停止有限的css动画。但是,如何为无限动画实现相同的效果?例如,对于以下代码段,当pointerup
被触发时,如何在360deg
状态停止动画?
document.addEventListener("pointerup", () => {
const div = document.getElementsByTagName("div")[0];
//
});

div {
width: 100px;
height: 100px;
background: blue;
position: relative;
animation: ani 5s infinite;
}
@keyframes ani {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

<div></div>
&#13;
pointerup
,但是首选的是包含纯css或最少js的解决方案。答案 0 :(得分:0)
答案 1 :(得分:0)
试试这个:
div = document.getElementsByTagName("div")[0];
div.addEventListener("pointerup", (ev) => {
ev.target.className = "stopme";
});
&#13;
div {
width: 100px;
height: 100px;
background: blue;
position: relative;
animation: ani 5s infinite;
}
div.stopme{
animation-fill-mode: forwards;
animation-iteration-count: 1;
}
@keyframes ani {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
&#13;
<div></div>
&#13;