二、过渡事件和动画事件

二、过渡事件和动画事件

2.1 过渡事件

当一个元素过渡事件的开始、经过、结束,会触发一个事件

1
2
3
transitionstart
transitionrun
transitionend
1
2
3
4
5
6
7
8
// 获取元素
var box = document.getElementById("box");
// 2s 之后让box加cur
setTimeout(function(){
// box.className = "cur";
// 也可以使用setAttribute添加类名
box.setAttribute("class", "cur");
}, 2000);
1
2
3
4
5
6
7
8
9
10
// 过渡事件
box.addEventListener("transitionstart", function(){
console.log("过渡开始");
});
box.addEventListener("transitionrun", function(){
console.log("过渡中");
});
box.addEventListener("transitionend", function(){
console.log("过渡完成");
});
1
2
3
4
5
6
7
8
9
10
11
12
#box {
position: absolute;
width: 100px;
height: 100px;
background-color: red;
left: 0;
top: 0;
transition: all 2s ease 0s;
}
#box.cur {
left: 100px;
}

U89Z6K.png

2.2 动画事件

当一个动画执行的时候会触发两个事件(重复执行会再触发一个事件)

1
2
3
animationstart:事件在 CSS 动画开始播放时触发。
animationiteration:事件在 CSS 动画重复播放时触发。
animationend:事件在 CSS 动画结束播放时触发。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#box {
position: absolute;
width: 100px;
height: 100px;
background-color: red;
left: 0;
top: 0;
/* 执行动画 */
animation: go 1s linear 0s 5 alternate;
}
/* 添加动画事件 */
@keyframes go {
from{
left: 0;
}
to {
left: 100px;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
// 动画开始
box.addEventListener("animationstart", function(){
console.log("动画开始");
});
// 动画重复播放
box.addEventListener("animationiteration", function(){
console.log("动画重复播放");
});
// 动画结束
box.addEventListener("animationend", function(){
console.log("动画结束");
});

U89KTH.png

点击查看

本文标题:二、过渡事件和动画事件

文章作者:Mango

发布时间:2020年07月14日 - 13:10:42

最后更新:2020年07月14日 - 13:27:10

原始链接:https://mango185.github.io/post/c8eb6eeb.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------