如何用js使得一个已经结束的css的animation动画重新执行一遍

好像要关掉动画然后再重新开启动画可以重新执行,那怎么样才算是开启和关闭动画呢... 好像要关掉动画然后再重新开启动画可以重新执行,那怎么样才算是开启和关闭动画呢 展开
 我来答
球球的前端奶茶屋
2016-02-10 · TA获得超过665个赞
知道小有建树答主
回答量:229
采纳率:0%
帮助的人:292万
展开全部

可以试试移除动画的类再重新给节点添加动画的类

下面这个demo是执行一次动画,2s后再重新执行一遍

(因为是demo,我就没有考虑兼容性问题,没有添加css前缀)

<div class="dot anm" id="anm"></div>
.dot {
    position: relative;
    width: 100px;
    height: 100px;
    margin: 100px;
    border-radius: 50%;
    background: #000;
}
.anm {
    animation: move 1s forwards;
}
@keyframes move
{
    from {
        left:0px;
    }
    to {
        left:200px;
    }
}
setTimeout(function() {
    var dom = document.getElementById('anm');
    dom.className = 'dot';
    setTimeout(function() {
        dom.className = 'dot anm';
    }, 10);
}, 2000);
Kenn
推荐于2018-03-10 · TA获得超过517个赞
知道小有建树答主
回答量:703
采纳率:82%
帮助的人:186万
展开全部
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box { width: 400px; height: 400px; background-color: red;  }
.animation {
animation: rotate 2s both linear;
}
@keyframes rotate {
form {
transform: rotate(0deg)
}
to { transform: rotate(360deg) }
}
</style>
</head>
<body>


<div class="box">
box
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
var box = document.querySelector('.box');
box.classList.add('animation');
box.addEventListener('animationend', function(e) {
var self = this
self.classList.remove('animation')
setTimeout(function() {
self.classList.add('animation')
}, 1)
})
})
</script>

</body>
</html>

动画播放结束的事件是animationend

如果你只是要无限循环的话,不用javascript,css animation这样写

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box { width: 400px; height: 400px; background-color: red;  }
.animation {
animation: rotate 2s both linear infinite;
}
@keyframes rotate {
form {
transform: rotate(0deg)
}
to { transform: rotate(360deg) }
}
</style>
</head>
<body>


<div class="box animation">
box
</div>


</body>
</html>
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式