当前位置: 移动技术网 > IT编程>网页制作>CSS > css3动画事件—webkitAnimationEnd与计时器time事件

css3动画事件—webkitAnimationEnd与计时器time事件

2019年07月25日  | 移动技术网IT编程  | 我要评论

闯官场,十七岁的单车下载,新人成神之路

用css3的animation完成一个动画,当只有这个动画完成时才执行令一个事件,比如让动画保持在终止的状态或其他一些事件。我们该怎么办呢。
第一种方法
用计时器,设定一个和动画时长一样的time,过time事件去执行这个函数。
settimeout(function(){ },time);
第二种方法
当-webkit-animation动画结束时有一个webkitanimationend事件,只要监听这个事件就可以了。
例子:

复制代码
代码如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="@my_programmer">
<title>webkitanimationend</title>
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="format-detection" content="telephone=no"/>
<style type="text/css">
#div{
width:200px;
height:200px;
background:#f60;
margin:100px auto;
-webkit-transition: all ease 1s;
}
.change{
-webkit-animation: transform 1s 2 ease;
}
@-webkit-keyframes transform {
% { -webkit-transform: scale(1)}
% { -webkit-transform: scale(2)}
% { -webkit-transform: scale(0.5)}
% { -webkit-transform: scale(1)}
}
</style>
</head>
<body>
<div id="div"></div>
<script type="text/javascript">
var tt = document.queryselector('#div');
tt.addeventlistener("click", function(){
this.classname = 'change';
}, false);
tt.addeventlistener("webkitanimationend", function(){ //动画结束时事件
this.classname = this.classname.replace('change', ' ');
console.log(2);
}, false);
</script>
</body>
</html>

拓展
1、-webkit-animation动画其实有三个事件:
开始事件 webkitanimationstart
结束事件 webkitanimationend
重复运动事件 webkitanimationiteration
你可以在上个例子中测试一下这两个事件

复制代码
代码如下:

tt.addeventlistener("webkitanimationstart", function(){ //动画开始时事件
console.log(1);//动画开始时,控制台输出1
}, false);
tt.addeventlistener("webkitanimationiteration", function(){ //动画重复运动时的事件
console.log(3);//第一遍动作完成时,控制台输出3
}, false);

2、css3的过渡属性transition,在动画结束时,也存在结束的事件:webkittransitionend;
注意:transition,也仅仅有这一个事件。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网