如何让javascript控制css3的animation和transition,让css3反复执行

html-css022

如何让javascript控制css3的animation和transition,让css3反复执行,第1张

通过按钮的click事件反复触发一个元素的css3动画,点击一次,动画效果就跑一次。

看码——

html:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>测试页面</title>

<script id="jquery_183" type="text/javascript" src="//runjs.cn/js/sandbox/jquery/jquery-1.8.3.min.js"></script>

</head>

<body>

    <div id="testDiv01">

    </div>

    <button id="testBtn01">反复触发transition</button>

    <br>

    <div id="testDiv02">

    </div>

    <button id="testBtn02">反复触发animation</button>

</body>

javascript:

let [testDiv01,testDiv02,testBtn01,testBtn02]=[$('#testDiv01'),$('#testDiv02'),$('#testBtn01'),$('#testBtn02')]

testBtn01.on('click',function () {

        testDiv01.addClass('transi')

        let t = setTimeout(()=>{

            testDiv01.removeClass('transi')

            clearTimeout(t)

        },500)

})

testBtn02.on('click',function () {

        testDiv02.addClass('ani')

        let t = setTimeout(()=>{

            testDiv02.removeClass('ani')

            clearTimeout(t)

        },500)

})

css:

body {

  padding: 20px

}

.testDiv {

  width: 100px

  height: 100px

  border-radius: 50%

  background-color: #e0a718

}

.testDiv.ani {

  -webkit-animation: pop 200ms ease 0ms

          animation: pop 200ms ease 0ms

}

.testDiv.transi {

  -webkit-transform: scale(1.2)

          transform: scale(1.2)

  -webkit-transition: -webkit-transform 0.5s

  transition: -webkit-transform 0.5s

  transition: transform 0.5s

  transition: transform 0.5s, -webkit-transform 0.5s

}

.testBtn {

  margin-top: 20px

  height: 30px

  padding: 0px 10px

  border: 1px solid #CCCCCC

}

@-webkit-keyframes pop {

  0% {

    -webkit-transform: scale(0)

            transform: scale(0)

  }

  50% {

    -webkit-transform: scale(1.2)

            transform: scale(1.2)

  }

  100% {

    -webkit-transform: scale(1)

            transform: scale(1)

  }

}

@keyframes pop {

  0% {

    -webkit-transform: scale(0)

            transform: scale(0)

  }

  50% {

    -webkit-transform: scale(1.2)

            transform: scale(1.2)

  }

  100% {

    -webkit-transform: scale(1)

            transform: scale(1)

  }

}

一、实现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

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