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

html-css0124

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

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

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

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

1

<div class="dot anm" id="anm"></div>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

.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)

一、实现CSS3无限循环动画代码示例。

代码如下:

CSS:

@-webkit-keyframes gogogo {

0%{

-webkit-transform: rotate(0deg)

border:5px solid red

}

50%{

-webkit-transform: rotate(180deg)

background:black

border:5px solid yellow

}

100%{

-webkit-transform: rotate(360deg)

background:white

border:5px solid red

}

}

.loading{

border:5px solid black

border-radius:40px

width: 28px

height: 188px

-webkit-animation:gogogo 2s infinite linear

margin:100px

}

扩展资料

实现动画无限循环所需要的CSS属性说明:

1、infinite

在animation后面加上infinite就可以无限循环,另外还可以做反向循环使用animation-direction

2、animation-name

规定需要绑定到选择器的 keyframe 名称。

3、animation-duration

规定完成动画所花费的时间,以秒或毫秒计。

4、animation-timing-function

规定动画的速度曲线。

5、animation-delay

规定在动画开始之前的延迟。

6、animation-iteration-count

规定动画应该播放的次数。

7、animation-direction

规定是否应该轮流反向播放动画。